Java OCA OCP Practice Question 1351

Question

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

  • A. The catch block for Exception must appear before the catch block for IOException.
  • B. The catch block for IOException must appear before the catch block for Exception.
  • 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.


B.

Note

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

If Exception was to appear before IOException, then the IOException block would be considered unreachable code because any thrown IOException is already handled by the Exception catch block.

Option B is correct.




PreviousNext

Related