Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2005

Tip: Looking for answers? Try searching our database.

Java Exception Please Help!

Thread view: 
toy - 07 Nov 2005 10:49 GMT
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
Fabien Bergeret - 07 Nov 2005 10:52 GMT
> Miller_Rabin.java:24: unreported exception java.io.IOException; must be
> caught or declared to be thrown
[quoted text clipped - 9 lines]
>
> I can provide additional details if necessary

try {
    String t = stdin.readLine();
    s = Integer.parseInt(t);
}
catch(IOException e){
    System.err.println("Unable to read from stdin");
}
catch(NumberFormatException e) {
    System.err.println("What you typed is definitely not a number");
}
toy - 07 Nov 2005 20:02 GMT
Thanks for your help fabien...can you help me with the same string
error...what do I do?
Bryce - 07 Nov 2005 14:17 GMT
>Miller_Rabin.java:24: unreported exception java.io.IOException; must be
>caught or declared to be thrown
[quoted text clipped - 9 lines]
>
>I can provide additional details if necessary

 try {
   String t = stdin.readLine();
   s = Integer.parseInt(t);
 } catch(IOException e) {
   // Do something here
 }

--
now with more cowbell
toy - 07 Nov 2005 19:14 GMT
what about this line:

  String ns = stdin.readLine();

Generatese the same error, except it is a string.
Oliver Wong - 07 Nov 2005 19:45 GMT
> what about this line:
>
>   String ns = stdin.readLine();
>
> Generatese the same error, except it is a string.

   You might want to read Sun's tutorial on exceptions:

http://java.sun.com/docs/books/tutorial/essential/exceptions/

   - Oliver
Ted Present - 07 Nov 2005 21:53 GMT
Try adding "throws IOException" after the method declaration

For example...

<code>
public static void main(String[] args) throws IOException
{
   String str = Integer.parseInt(stdin.readLine());
}
</code>

This should at least allow the method to compile.  If you want to handle the
error in a specific way, however, use the try...catch block as others have
said.

Signature

Ted Present
presentt@verizon.net

Noodles Jefferson - 08 Nov 2005 04:42 GMT
> Miller_Rabin.java:24: unreported exception java.io.IOException; must be
> caught or declared to be thrown
[quoted text clipped - 9 lines]
>
> I can provide additional details if necessary

No need.

The error message is telling you that you need to either:

a) put throws IOException in your method header

public void whatever() throws IOException {

}

or

b) put a try/catch block in your method.

public void whatever() {

try {

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

} catch (IOException ioe) {

// do something

}

Signature

Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.