Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Shafeeq Sheikh
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic