This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IOException / Exception   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IOException / Exception   " Watch "IOException / Exception   " New topic

IOException / Exception

Naveen Sharma
Ranch Hand

Joined: Mar 23, 2001
Posts: 65
Hi All
I M Still Not Clear About The Concept.Its OK That Complier wont give Error in Case of Runtime Exceptions as we already Know.
But Why it is specifically giving error on IOException.Even with Exception It works Perfectly. e.g
class Temp
public static void main(String args[])
catch(Exception e)
} //Compiles and Run
class Temp
public static void main(String args[])
catch(IOException e)
} //Compile Time Error

Please Explain Why So..

Shallu Shankar

Joined: Apr 20, 2001
Posts: 15
Hi Naveen ,
It is because IOException is not in java.lang package . It is in package .
Include this statement in your program and it will run fine :
Naveen Sharma
Ranch Hand

Joined: Mar 23, 2001
Posts: 65
Sorry I Forgot write to import*;
but my dear friend even if i write it . It still gives Error
Dont Know Why
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
hi navenn,
see u need to write some code in the try statement which can throw an IOException.If u write Exception or throwable instead of IOException in your catch block,your code will run fine.
Santosh Pandey

Joined: Jul 17, 2000
Posts: 3
It will not give compile error if you catch any RuntimeException or its subclasses(bcoz a RuntimeException may occur for any running program). Since Exception is super class to RuntimeException you didnt get error. Even if you catch RuntimeException it will not give any error.
If you try to catch an exception (like IOException)which might occur only for a certain type of statements, compiler objects, bcoz there is no such statement.
Try using any subclass of RuntimeException, compiler won't object.
I hope it helps.

Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Santosh's answer is almost true. Actually I would describe it as : All checked exceptions should be actually thrown in try block before they can be used in catch block.
Since IOException is checked exception and Exception is not a checked exception it explains the scenario of code being compiled in one case and not in other. Since Java compiler gets more picky about checked exception it doesn't allow the code to be compiled.
Hope it helps.
Gaurav Mantro

----------------------<br />regards.<br /><a href="" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="" target="_blank" rel="nofollow"></a>
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Unchecked exceptions ie., subclasses of Error and RuntimeException usually indicate unrecoverable error conditions. You could catch them in the program, catch them in the program but may not be able to do much in terms of graceful exit!
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Dave Terrian

Joined: Jun 25, 2000
Posts: 15
This might be useful:
Runtime Exceptions--The Controversy, part of the Essential Java Classes trail of the Java Tutorial

[This message has been edited by Dave Terrian (edited April 27, 2001).]
I agree. Here's the link:
subject: IOException / Exception
Similar Threads
Exception Doubt..
error thrown in a constructor..
Exception confusion
Exception test
Question on Exception