Java Exception Please Help

T

toy

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
 
H

hilz

toy said:
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
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top