File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IOException / Exception

 
Naveen Sharma
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Terrian
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic