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
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
posted
0
Sorry I Forgot write to import java.io.*; but my dear friend even if i write it . It still gives Error Dont Know Why
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.
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.
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
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).