Just to add a bit more context:
Java has the concept of "checked exceptions". These are all exceptions that extend
Exception, except for those that extend
RuntimeException. They are "checked" because the compiler forces you to handle them. If you execute any code that might throw a checked exception, you have two options:
- Handle it there and then, in a
try/
catch block
- Declare your method as throwing that exception, using the
throws statement. This forces the next calling method above to deal with it (again, using one of the two approaches).
(these correspond to the two approaches Kemal and Atul mention).
The
PrintWriter constructor that takes a
String as an argument has the declaration
throws FileNotFoundException - see the Javadocs at
java.io.PrintWriter#PrintWriter(java.lang.String). That's a checked exception, so you have to use one of the two approaches listed above.
See the
Java Tutorials for more on exceptions.