The compiler is
supposed to guard against catch blocks that can never be entered. If the try block cannot throw the indicated exception, then the catch block is unreachable code and should generate a compile-time error.
Specifically, section 14.20 of the
Java Language Specification states in part, "A catch block C is reachable iff [if and only if]... some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C..."
Ref:
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#236365 However, there is a known bug that does allow such code to compile under certain conditions:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4046575 [ April 25, 2005: Message edited by: marc weber ]