wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions 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 "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Hello everybody.....
I have a basic doubt on Exceptions..... hope somebody out there can clear it off....
I have two pieces of code.... The first one compiles fine...

public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Throwable t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}

The second piece of code has an IOException in the catch clause:

import java.io.*;
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}

This piece gives a compiler error saying:
C:\java\bin>javac Throw.java
Throw.java:11: Exception java.io.IOException is never thrown in the body of the corresponding try statement.
catch(IOException t)
^
1 error
What I do not understand is why does the compiler perform this check only for subclasses of the Throwable class and not for the Throwable class itself.....
Please help me here.....

Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
Hello.
Actually Class throwable is a generalize class which covers all type of exceptions but IOEXception is a specific type exception which is definately the subclass of throwable but SPECIFIC .

Regards
Khurram Fakhar
Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
Hello.
Actually Class throwable is a generalize class which covers all type of exceptions but IOEXception is a specific type exception which is definately the subclass of throwable but SPECIFIC .

Regards
Khurram Fakhar
Ankur Gupta
Ranch Hand

Joined: Jun 13, 2000
Posts: 66
Shafeeq,
I believe this is because in the first case i.e. when it is Throwable then the compiler does not know whether it is an Error or an Exception(because both runtime and checked Exceptions derive from Throwable). In the second case it is specifically told that the method in the try-catch block is throwing an IOException, but actually there is no method which is doing so. Compiler will check for the code in there and try to figure out whether it is capable of throwing the said Exception. If not then it will flag it saying "it is never thrown in the body of the try-catch".
Ankur
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Exceptions