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!
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.
Re: Error:unreported exception java.io.IOexception; must be caught or declared to be
And how do I implement a catch for the method??
Re: Error:unreported exception java.io.IOexception; must be caught or declared to be
Quote:
Originally Posted by
misterbreadcrum
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
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"
Re: Error:unreported exception java.io.IOexception; must be caught or declared to be
Quote:
Originally Posted by
coasterguy10
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.
Re: Error:unreported exception java.io.IOexception; must be caught or declared to be
And, when handling it, at least do a printStackTrace().
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());
}
}
}