Results 1 to 8 of 8
- 09-20-2011, 10:30 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 5
- Rep Power
- 0
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!
- 09-20-2011, 10:37 PM #2
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.
- 09-20-2011, 11:18 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 5
- Rep Power
- 0
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
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: ExceptionsLast edited by Fubarable; 09-20-2011 at 11:38 PM.
- 09-21-2011, 02:43 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
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
- 09-21-2011, 10:02 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Error:unreported exception java.io.IOexception; must be caught or declared to be
And, when handling it, at least do a printStackTrace().
- 03-21-2012, 08:07 AM #8
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
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
-
unreported exception java.io.IOException
By fluffaykitties in forum New To JavaReplies: 11Last Post: 03-07-2011, 01:59 AM -
unreported exception java.lang.Exception; must be caught or declared to be thrown
By arc_remiel in forum New To JavaReplies: 5Last Post: 02-14-2011, 11:39 PM -
unreported IOException problem
By doha786 in forum New To JavaReplies: 5Last Post: 04-20-2010, 11:14 PM -
unreported exception IOException -- Yet I AM catching it?
By Agathorn in forum New To JavaReplies: 2Last Post: 09-18-2009, 11:22 PM -
Error: unreported exception java.io.IOException; ??
By jonsamwell in forum New To JavaReplies: 5Last Post: 08-24-2008, 04:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks