Two Laptop Bag*
The moose likes Beginning Java and the fly likes throws IOException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "throws IOException?" Watch "throws IOException?" New topic
Author

throws IOException?

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
Hi.

I have two classes: Logic.java and Main.java

In Main.java, a file is selected and the absoulute path is returned. This should then be passed to the Logic.java class which will read the top line of that file and return that as a String back to the Main.java class to be displayed in a jTextField. The method within Main.java that should do this reads:

In Logic.java, I am reading the top line and am trying to get it to return to the jTextField. The code for this reads:

However, I get an error message that reads:

unreported exception java.io.IOException; must be caught or declared to be thrown
jTextField1.setText(myLogic.readTopLine());

What am I doing wrong. I can't figure out where else I need to put the throws IOException statement.

Many thanks
[ April 26, 2007: Message edited by: Sam Bluesman ]

Moosey knows best
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
You need a trip to the Java Tutorial here, to read all about Exceptions. This is a simplified inheritance hierarchy for ExceptionsNow the classes I have printed in bold wiht asterisks count as checked Exceptions, and the others (Error and RuntimeException and its subclasses) count as unchecked. This means that the compiler absolutely insists it see error-handling for the checked sort. The commonest checked Exception is IOException.

If you get that error from the compiler, it means you have called something which has "throw new IOException();" somewhere in its code. So you must either throw it ("specify requirement") back up the call stack for another method to deal with, or you must catch and deal with the Exception in the method where the compiler warning occurred ("catch requirement").

You have done one stage of that, specifying the Exception in the readTopLine method; you now have to handle it in whichever method called readTopLine, or change readTopLine to handle the Exception there.

The handling in the calling method may allow you to specify it again and pass the Exception farther up the call stack.

BTW: Two more things in readTopLine: You are getting a String from the readLine() method, then returning toString(). Why?
You MUST close the FileReader and BufferedReader when you have finished with them. If you don't know how to do that with a finally block, please start a new thread.
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
Hi Sam,

you call

, which throws the IOException, in

Since that latter line is not encapsulated in a try/catch, and the surrounding method

does not throw the exception, you get the error report you get.

So either propagate the exception by adding a throws declaration to the enclosing method

, which will force you to catch the exception in the code invoking jMenuItemOpenActionPerformed, or enclose

in a try/catch block:


[ April 26, 2007: Message edited by: Guido Sautter ]
[ April 26, 2007: Message edited by: Guido Sautter ]
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
Thanks to the both of you for your help
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
You're welcome.

BTW: I hope you could read my inheritance tree; on Windows with Mozilla it won't show the bold; on Mozilla on Linux it shows bold but some of the / and \ move a bit to the left.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: throws IOException?