Results 1 to 6 of 6
  1. #1
    jonsamwell is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

    Default 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());

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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());

  3. #3
    jonsamwell is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

    Default

    thanks,

    but why does it have to be enclosed in a try catch statement in the first place?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    unreported exception java.io.IOException; must be caught or declared to be thrown"
    Because the compiler says so! Makes for more robust programs if you catch errors and not let them terminate your jvm.

  5. #5
    udayadas's Avatar
    udayadas is offline Member
    Join Date
    Aug 2008
    Posts
    22
    Rep Power
    0

    Default

    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

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. Replies: 3
    Last Post: 04-10-2008, 09:01 AM
  2. java.io.IOException: invalid header field
    By osval in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 11:09 PM
  3. Variable passing, Error: IOException
    By fernando in forum New To Java
    Replies: 3
    Last Post: 07-31-2007, 02:03 PM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 06:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •