java checked exception in a catch clause compilation error
- by srandpersonia
Hi, I was expecting an compilation error in the following program because of the throw statement in the catch block as IOException is a checked exception and it is not caught by another try block within the catch block. But I am getting "Hurray!" printed. Any explanation would be much appreciated.
According to JLS 11.2.3,
http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html
It is a compile-time error if a method
or constructor body can throw some
exception type E when both of the
following hold:
* E is a checked exception type
* E is not a subtype of some type declared in the throws clause of the
method or constructor.
import java.io.*;
public class Test{
public static void main(String args[])
{
System.out.println(method());
}
public static int method()
{
try{
throw new Exception();
}
catch(Exception e){
throw new IOException(); //No compile time error
}
finally{
System.out.println("Hurray!");
}
}
}
Thanks in advance.