Amateurish Java question

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
I was completing an assignment for a java class dealing with user input using InputStreamReader. I'm sorry I don't have the source code to attach (I'm at work).

In a method outside main(), I had planned on inserting the code for the InputStreamReader. I dutifully inserted the statement throws java.io.IOException before the braces enveloping the method body. I kept getting a compiler error indicating the exceptions had to be referenced or something to that effect (again I don't have the code handy to say specifically for which I apologize). When I would put the code for the InputStreamReader in the main() method, and the throws statement before the main() method body I wouldn't get that problem. I had hoped to put the ISR code in a separate method and make calls to that method from within main(). I got it to work fine by putting all the code in the main method, but was just wondering why I was getting that compiler error. My syntax was fine in the code. I can't believe throws statements can only be used in conjunction with main methods, so why is this happening? FWIW I am using jGrasp as the IDE.

Any insight into this is appreciated, I already turned the assignment in, so nobody answering is allowing me to cheat.

Thanks
 

Cooler

Diamond Member
Mar 31, 2005
3,835
0
0
We need the code to debug.

Could be static method call from a non static fuction.

we also need the exact error msg if we have no code to debug.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Why don't you just catch the exceptions, instead of letting them pop up the call stack?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Err, far more likely is simply that he didn't handle the exception in the main method. (Edit: this was in response to the first reply)

Op, when a method declares that it might throw an exception, any method that calls that method must do one of two things: either catch the exception and handle it, or declare that it too can throw the exception. So in this case, if your i/o method can throw an IOException, main() must either catch that exception (using a try/catch block, but I'm guessing that's beyond your level still) or put the same throws IOException in it's signature.

So the quick fix is to put the throws clause in both methods and it'll be just like you're doing the i/o in the main method. But your code will blow up if anything actually does go wrong. The more appropriate fix, if you want to put in the time to learn a little beyond what the class has taught you so far, is to learn to use try/catch so that you can give the user a nice error message.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
@diegoal: I would have done that, really I would, but I was in a rush and we haven't covered exception handling (I believe that's this week actually). From what you and kamper say, exception handling code would be the way to go.

I may go and try what kamper suggested as a quick fix and insert throws statements in both methods and see how that goes. But if the real-world solution would be exception handling code, I suppose that's what I should do.

And, again, thanks for the help, I know it's more difficult without the source code.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Actually, in the real world, if I was writing a program all in one class with static methods, I'd probably just let the IOException fly :p

I'd rather see an informative stack trace than waste keystrokes on an error message that will mean precious little to me. Of course, for anything bigger, it's a different story :)