File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Throwable, Exception and IOException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Throwable, Exception and IOException " Watch "Throwable, Exception and IOException " New topic

Throwable, Exception and IOException

Anselme Bender

Joined: May 22, 2002
Posts: 21
In the following class I can't understand why the compiler complains when using
catch( t)
where it doesn't with either :
catch(Throwable t) // OK
catch(Exception t) // OK
Is it the compiler responsability to do it that way ?
public class AQuestion
public static void main(String args[])
System.out.println("Before Try");
// Nothing here
// catch(Throwable t) // OK
// catch(Exception t) // OK
catch( t) // doesn't compile
System.out.println("Inside Catch");
System.out.println("At the End");

Anselme Bender<br />SCJP2
Anselme Bender

Joined: May 22, 2002
Posts: 21
One addition.
I should have written my question :
Why the compiler doesn't complain if the catch statement uses Throwable or Exception
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
Because the code inside the main method can throw a number of RuntimeExceptions (like NullPointerException etc..)
so you can attempt to catch Throwable and Exception (which are the super classes of Runtime exceptions).
However, IOException is a checked exception. Since your method isnt able and wont throw anyway in your code an IOException (cause println or anything else there doesnt throw that kind of exception), the compiler complains and tells you: " hey you cant possibly get that IOExcpetion! so why are you trying to catch it?!? "
It is sorta covered in the JavaRanch Style Guide.
subject: Throwable, Exception and IOException
It's not a secret anymore!