java checked exception in a catch clause compilation error

Posted by srandpersonia on Stack Overflow See other posts from Stack Overflow or by srandpersonia
Published on 2010-05-02T04:33:10Z Indexed on 2010/05/02 4:37 UTC
Read the original article Hit count: 283

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about exception-handling