File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IOException / Exception   " Watch "IOException / Exception   " New topic
Author

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[])
{
try{}
catch(Exception e)
}
} //Compiles and Run
class Temp
{
public static void main(String args[])
{
try{}
catch(IOException e)
}
} //Compile Time Error

Please Explain Why So..
Thanks

Shallu Shankar
Greenhorn

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

Joined: Mar 23, 2001
Posts: 65
Sorry I Forgot write to import java.io.*;
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
Greenhorn

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.


santosh
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Naveen.
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.
regards
Gaurav Mantro
------------------
http://www.mantrotech.com


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Ajith Kallambella
Sheriff

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
Greenhorn

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).]
 
GeeCON Prague 2014
 
subject: IOException / Exception