• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Amateurish Java question

EvilManagedCare

Senior member
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
 
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.
 
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.
 
@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.
 
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 😛

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 🙂
 
Back
Top