[Solved] Java: Exception, Inheritance Concepts Unclear [closed]


(D) It does not compile 😉

The second catch block is unreachable.

If you turn it around, and catch Y first you still should get a compiler warning, as Z is a checked exception and the compiler will detect that only the first catch clause can be reached. But the example will work, and “Error Y” will be printed.

Java always evaluates the catch clauses in the order they are defined, which is the reason you should define the most specific exceptions first, in this case catch (Y y) should come before catch (X x).

EDIT: In the first version I assumed that the compiler error “Unreachable code” can be turned off with special compiler settings. Thanks to Marko Topolnik for the clarification.

5

solved Java: Exception, Inheritance Concepts Unclear [closed]