Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Error:unreported exception java.io.IOexception; must be caught or declared to be thro

    My class assigned me to write a fractions class.

    //Colin Steele
    //CSC2310
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    public class Fractions
    {
    public static void main(String[] args)
    {
    Fraction f1 = new Fraction(1,1);
    int x;
    InputStreamReader input = new InputStreamReader(System.in);
    x=input.read();
    }
    }

    --------------------Configuration: <Default>--------------------
    C:\Users\Colin\Desktop\Stuff\CSC2310\Colinsteele.j ava:13: unreported exception java.io.IOException; must be caught or declared to be thrown
    x=input.read();
    ^
    1 error

    Process completed.

    I get the unreported exception: java.io.IOException;must be caught or declared to be thrown

    The method read is overloaded into the following
    read(char[]) int
    read(char[],int,int) int
    read(CharBuffer) int
    read() int

    This may be a simple answer but I just can't figure it out!

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: Error:unreported exception java.io.IOexception; must be caught or declared to be

    when you call read() on the InputStreamReader, you have to catch a java.io.IOException.

    Basically putting in a catch so that if an IOException was to occur, you can handle it however you want to (maybe log something, let the user know, etc), as opposed to having the code stop running.

  3. #3
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Error:unreported exception java.io.IOexception; must be caught or declared to be

    And how do I implement a catch for the method??

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Error:unreported exception java.io.IOexception; must be caught or declared to be

    Quote Originally Posted by misterbreadcrum View Post
    And how do I implement a catch for the method??
    Please look at the Java tutorials for questions like these. They have an excellent section on exception handling.

    Edit: if you haven't yet found it on Google, the link is here: Exceptions
    Last edited by Fubarable; 09-21-2011 at 12:38 AM.

  5. #5
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default Re: Error:unreported exception java.io.IOexception; must be caught or declared to be

    add "throws IOException" to your main line so it reads, "public static void main(String[] args) throws IOException"

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Error:unreported exception java.io.IOexception; must be caught or declared to be

    Quote Originally Posted by coasterguy10 View Post
    add "throws IOException" to your main line so it reads, "public static void main(String[] args) throws IOException"
    No. In general it is not a good idea to throw an exception from the main method. Again, read the tutorial, learn to catch the exception and handle it.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Error:unreported exception java.io.IOexception; must be caught or declared to be

    And, when handling it, at least do a printStackTrace().

  8. #8
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: Error:unreported exception java.io.IOexception; must be caught or declared to be

    Hi,

    you can try this one:-

    import java.io.*;
    import java.util.*;
    import java.awt.*;
    public class Fractions
    {
    public static void main(String[] args)
    {
    Fraction f1 = new Fraction(1,1);
    int x;
    InputStreamReader input = new InputStreamReader(System.in);

    try
    {
    x=input.read();
    }

    catch(IOException io)
    {
    System.out.println("IOException" + e.getMessage());
    }
    }
    }

Similar Threads

  1. unreported exception java.io.IOException
    By fluffaykitties in forum New To Java
    Replies: 11
    Last Post: 03-07-2011, 02:59 AM
  2. Replies: 5
    Last Post: 02-15-2011, 12:39 AM
  3. unreported IOException problem
    By doha786 in forum New To Java
    Replies: 5
    Last Post: 04-21-2010, 12:14 AM
  4. Replies: 2
    Last Post: 09-19-2009, 12:22 AM
  5. Error: unreported exception java.io.IOException; ??
    By jonsamwell in forum New To Java
    Replies: 5
    Last Post: 08-24-2008, 05:11 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
  •