Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throws IOException?

 
Sam Bluesman
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 191
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to the both of you for your help
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic