Results 1 to 6 of 6
- 08-23-2008, 05:27 PM #1
Member
- Join Date
- Aug 2008
- Posts
- 11
- Rep Power
- 0
Error: unreported exception java.io.IOException; ??
Hi,
I'm just learning java so bare with me!! i'm just modify some examples i've read through in a book and keep getting the following error at the if statement. could someone tell me what it means and the possible solution??
Error: "unreported exception java.io.IOException; must be caught or declared to be thrown"
Thank you very much
Jon
This is all in public static void main:
Java Code:double transaction; String line; java.io.BufferedReader stream_in; stream_in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); System.out.println("Input the transaction amount."); System.out.flush(); if ((line=stream_in.readLine()) !=null) //the error appears here? transaction = Double.valueOf(line).doubleValue(); else transaction = 0; wallet[0].chargeIt(transaction); System.out.println("New Balance: £ " + wallet[0].getBalance());
- 08-23-2008, 05:53 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Enclose with the try-catch block and see. It'll fix.
Java Code:double transaction = 0; String line; try { java.io.BufferedReader stream_in; stream_in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); System.out.println("Input the transaction amount."); System.out.flush(); if ((line = stream_in.readLine()) != null) { //the error appears here? transaction = Double.valueOf(line).doubleValue(); } else { transaction = 0; } } catch (IOException ex) { ex.printStackTrace(); } wallet[0].chargeIt(transaction); System.out.println("New Balance: £ " + wallet[0].getBalance());
- 08-23-2008, 08:25 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 11
- Rep Power
- 0
thanks,
but why does it have to be enclosed in a try catch statement in the first place?
- 08-23-2008, 09:13 PM #4
Because the compiler says so! Makes for more robust programs if you catch errors and not let them terminate your jvm.unreported exception java.io.IOException; must be caught or declared to be thrown"
- 08-23-2008, 11:00 PM #5
There are two types of exceptions:
(UE)Unchecked Exceptions (Ex : ArithmaticException)
(CE)Checked Exceptions (Ex : SQLException , IOException etc)
If you using any code that may throw UE then you need not handle them.
On the other hand if such code may throw CE then the compiler will force you to handle them or propagate them.
How to Handle:
============
Put such code in the try/catch block
How to propagate them:
============
Use throws clause in the method declaration
- 08-24-2008, 04:11 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I think it's better to read some materials related to exception handling in Java, jonsamwell. You get here some well explanations from Norm and udayadas.
Hope you have solve the issue. Any other question?
Similar Threads
-
java.io.IOException: CreateProcess: matlab error=2
By Jack in forum Advanced JavaReplies: 3Last Post: 04-10-2008, 09:01 AM -
java.io.IOException: invalid header field
By osval in forum Advanced JavaReplies: 1Last Post: 08-06-2007, 11:09 PM -
Variable passing, Error: IOException
By fernando in forum New To JavaReplies: 3Last Post: 07-31-2007, 02:03 PM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM -
JSF error+exception
By Peter in forum SWT / JFaceReplies: 1Last Post: 07-04-2007, 06:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks