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.
The moose likes Beginning Java and the fly likes help compiling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help compiling" Watch "help compiling" New topic

help compiling

charlie mills
Ranch Hand

Joined: Nov 01, 2008
Posts: 49
I've written this code but it's not compiling. The error message is saying 'unreported exception 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

Joined: Sep 28, 2004
Posts: 20019

The error message is saying 'unreported exception 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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Pradeep Mudaliar

Joined: Oct 11, 2007
Posts: 23
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.

SCJP 6 - 80%
My Blog -
I agree. Here's the link:
subject: help compiling
It's not a secret anymore!