Difficulty: Easy
Correct Answer: throws Exception
Explanation:
Introduction / Context:
This question focuses on Java's checked exception rules. A method that calls another method which declares a checked exception must either handle it with a try/catch or declare it in its own throws clause with the same type or a supertype.
Given Data / Assumptions:
throws TestException
, which is a checked exception.
Concept / Approach:
For checked exceptions, the compiler enforces one of two options: catch the exception locally, or declare it. Declaring throws Exception
on test() is legal because Exception is a supertype of TestException. A try/catch could work too, but the prompt asks what to add “at Point X” in the method signature, not inside the body.
Step-by-Step Solution:
Identify the thrown type: TestException extends Exception.To compile, test()
must either catch or declare.Adding throws Exception
to the signature satisfies the rule because it covers TestException.Therefore, the correct addition is a throws clause with Exception (or TestException).
Verification / Alternative check:
Replace with throws TestException
and recompile; it also succeeds. Wrapping in try { runTest(); } catch(TestException e){}
would also compile.
Why Other Options Are Wrong:
Option A: Compiler error persists without handling/declaring.Option C: A catch cannot be added at the signature point; it belongs inside the method body.Option D: Declaring RuntimeException does not satisfy checked-exception requirements.Option E: final
is unrelated.
Common Pitfalls:
Thinking any throws clause will do; it must be the same checked type or a supertype.
Final Answer:
throws Exception
Discussion & Comments