• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unreported exception

 
Ranch Hand
Posts: 42
Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am getting this error, but I don't know what it means. I've copied the exercise straight off the example, but it won't compile. What am I doing wrong? I haven't learned about exceptions, yet so I'm not trying to throw any.

FileSave.java:12: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
PrintWriter outputFile = new PrintWriter(filename);
^
1 error



 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And error message says that PrintWriter throws one, so when you declare it you need to put it in try block or try-with-resources statement. Another option would be to declare your method to throw the exception but I would recommend one of the first two options.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two ways to handle this error:-
1) To add throws after method declaration like wise



2) declare this variable enclose with try and catch block

[Added code tags - see UseCodeTags for details]


Thanks
Atul Itankar

InfoCepts | www.infocepts.com
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic