• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help compiling

 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written this code but it's not compiling. The error message is saying 'unreported exception java.io FileNotFoundException; must be caught or declared to be thrown.
I've only just learnt about creating your own methods and i'm struggling with it a bit, but i think what i've done is right.

 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message is saying 'unreported exception java.io FileNotFoundException; must be caught or declared to be thrown.


In Java, certain methods are defined to throw checked exception conditions. These are exceptions that occur, that you can't just ignore -- you either have to deal with them yourself (using try-catch clause) or you have to declare that they are not handled (and that your method can throw those exceptions). The file not found exception is one of those exceptions -- either you must handle it or your method must be declared to throw it.

Henry
 
Pradeep Mudaliar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surround the block of code which instantiates and uses a File Object with a try-catch block as follows -



As also mentioned by Henry, the other way of taking care of checked exceptions is to throw them from the method instead of handling them. So, you can have the controlMenu throw the Exception as follows -



If you go ahead with this approach, methods which invoke controlMenu() would have to handle or throw the exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic