Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-23-2008, 05:27 PM
Member
 
Join Date: Aug 2008
Posts: 11
Rep Power: 0
jonsamwell is on a distinguished road
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:

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());
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 08-23-2008, 05:53 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 9,394
Rep Power: 14
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
Enclose with the try-catch block and see. It'll fix.

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());
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.

Forums FAQ|Use CODE Tags|How To Ask Questions The Smart Way|The Java Tutorials|Glossary for Java|NetBeans IDE|Sun Downloads
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-23-2008, 08:25 PM
Member
 
Join Date: Aug 2008
Posts: 11
Rep Power: 0
jonsamwell is on a distinguished road
Default
thanks,

but why does it have to be enclosed in a try catch statement in the first place?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-23-2008, 09:13 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SouthWest Missouri, USA
Posts: 4,678
Rep Power: 7
Norm is on a distinguished road
Default
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-23-2008, 11:00 PM
udayadas's Avatar
Member
 
Join Date: Aug 2008
Posts: 22
Rep Power: 0
udayadas is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-24-2008, 04:11 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 9,394
Rep Power: 14
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.

Forums FAQ|Use CODE Tags|How To Ask Questions The Smart Way|The Java Tutorials|Glossary for Java|NetBeans IDE|Sun Downloads
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.io.IOException: CreateProcess: matlab error=2 Jack Advanced Java 3 04-10-2008 09:01 AM
java.io.IOException: invalid header field osval Advanced Java 1 08-06-2007 11:09 PM
Variable passing, Error: IOException fernando New To Java 3 07-31-2007 02:03 PM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-25-2007 10:55 PM
JSF error+exception Peter SWT / JFace 1 07-04-2007 06:29 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:44 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org