Java OCA OCP Practice Question 641

Question

If a try statement has catch blocks for both ClassCastException and RuntimeException, then which of the following statements is correct?

  • A. The catch block for ClassCastException must appear before the catch block for RuntimeException.
  • B. The catch block for RuntimeException must appear before the catch block for ClassCastException.
  • C. The catch blocks for these two exception types can be declared in any order.
  • D. A try statement cannot be declared with these two catch block types because they are incompatible.


A.

Note

ClassCastException is a subclass of RuntimeException, so it must appear first in any related catch blocks.

If RuntimeException was to appear before ClassCastException, then the ClassCastException block would be considered unreachable code,

since any thrown ClassCastException is already handled by the RuntimeException catch block.

Option A is correct.




PreviousNext

Related