Difficulty: Easy
Correct Answer: catch(X x) can catch subclasses of X where X is a subclass of Exception.
Explanation:
Introduction / Context:
This item tests core Java exception-handling concepts: the Throwable hierarchy, catch clause matching, and when try blocks are required. Understanding how the catch type relates to thrown exceptions is essential for writing robust error handling code.
Given Data / Assumptions:
Concept / Approach:
Java chooses a catch block based on the thrown object's runtime type. A catch parameter typed to a superclass will match any instance of that type or its subclasses. Errors are not RuntimeException; both Error and Exception extend Throwable independently. Checked exceptions must be caught or declared; unchecked (RuntimeException and Error) need not be caught or declared.
Step-by-Step Solution:
Identify hierarchy: Throwable → {Error, Exception}; RuntimeException extends Exception, not Error.Catch matching: catch(X) matches X and any subclass of X.Try requirement: only checked exceptions force try/catch or throws; Error is unchecked.Therefore, only the statement about catch(X x) catching subclasses of X is correct.
Verification / Alternative check:
Write a small program that throws a subclass of X and observe that catch(X) handles it; attempting to equate Error with RuntimeException will not compile logically or by documentation.
Why Other Options Are Wrong:
Option B: Error is not a RuntimeException.Option C: Errors are unchecked; no try required.Option D: You may declare throws instead of enclosing in try for checked exceptions.Option E: Incorrect because option A is true.
Common Pitfalls:
Confusing unchecked Errors with checked Exceptions; assuming try is always required; misunderstanding catch polymorphism.
Final Answer:
catch(X x) can catch subclasses of X where X is a subclass of Exception.
Discussion & Comments