This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
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.