aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Throwable, Exception and IOException " Watch "Throwable, Exception and IOException " New topic
Author

Throwable, Exception and IOException

Anselme Bender
Greenhorn

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


Anselme Bender<br />SCJP2
Anselme Bender
Greenhorn

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?!? "
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwable, Exception and IOException