File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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 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
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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


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

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 - http://www.polymorphiccoder.blogspot.com
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help compiling