Java Exception Please Help

Discussion in 'Java' started by toy, Nov 7, 2005.

  1. toy

    toy Guest

    Miller_Rabin.java:24: unreported exception java.io.IOException; must be
    caught or declared to be thrown
    String t = stdin.readLine();


    I'm not sure how to fix this error. someone please help. I have a line
    in my code that says:

    String t = stdin.readLine();
    s = Integer.parseInt(t);

    which is the line that's generating the exception

    I can' provide additional details if necessary
    toy, Nov 7, 2005
    #1
    1. Advertising

  2. toy

    hilz Guest

    toy wrote:
    > Miller_Rabin.java:24: unreported exception java.io.IOException; must be
    > caught or declared to be thrown
    > String t = stdin.readLine();
    >
    >
    > I'm not sure how to fix this error. someone please help. I have a line
    > in my code that says:
    >
    > String t = stdin.readLine();
    > s = Integer.parseInt(t);
    >
    > which is the line that's generating the exception
    >
    > I can' provide additional details if necessary
    >



    This line is not throwing an exception.
    This line is causing a comile error because stdin.readLine can throw an
    exception at runtime, and need to be caught somewhere.
    The compiler error message is telling you what to do:
    the excpetion must be either caugth or declared to be thrown.
    so either do this:

    String t=null;
    try {
    t = stdin.readLine();
    }catch(java.io.IOException exp){ exp.printStackTrace();}

    or have the method that this line is called from throw this exception..
    and then you have to catch it somewhere else....

    it would help to look at the Exceptions documentation on the
    java.sun.com website at this time..

    HTH
    hilz, Nov 7, 2005
    #2
    1. Advertising

  3. toy

    Roedy Green Guest

    On 7 Nov 2005 02:47:04 -0800, "toy" <> wrote, quoted
    or indirectly quoted someone who said :

    >String t = stdin.readLine();
    > s = Integer.parseInt(t);
    >
    >which is the line that's generating the exception


    It not actually generating an exception, it just POTENTIALLY could. so
    you wrap all your io code in something as simple as this:

    try {

    ...code that does I/O...

    }
    catch ( IOException e )
    {
    System.err.println(">>>>" + e.getMessage() );
    e.printStackTrace();
    }

    You don't normally wrap a single line in a try catch.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 7, 2005
    #3
  4. toy wrote:

    > Miller_Rabin.java:24: unreported exception..


    Oh, way yes it was reported - over on c.l.j.help.

    Please refrain from multi-posting.
    <http://www.physci.org/codes/javafaq.jsp#xpost>
    Andrew Thompson, Nov 7, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. queen
    Replies:
    3
    Views:
    6,593
    queen
    Jul 15, 2004
  2. Replies:
    4
    Views:
    506
    Chris Uppal
    May 5, 2005
  3. KK
    Replies:
    2
    Views:
    550
    Big Brian
    Oct 14, 2003
  4. MuZZy
    Replies:
    7
    Views:
    1,746
    Mike Hewson
    Jan 7, 2005
  5. mera
    Replies:
    7
    Views:
    1,100
Loading...

Share This Page