Difficulty: Easy
Correct Answer: Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
Explanation:
Introduction / Context:
This question tests core rules about Java exception handling: when finally
runs, whether a try
requires a catch
, and which exceptions must be declared or handled. Understanding these rules is essential for writing robust error-handling code.
Given Data / Assumptions:
Concept / Approach:
A finally
block is guaranteed to begin execution after the try
block starts, regardless of whether an exception is thrown, provided the JVM does not terminate abnormally before it can start. Unlike catch
, a finally
may exist without any catch
(i.e., try
+ finally
only). Errors are unchecked; they need neither declaration nor mandatory handling.
Step-by-Step Reasoning:
try
with a finally
but no catch
.Option B: False. Two identical catch clauses would make later ones unreachable (compile error).Option C: False. Error
and RuntimeException
are unchecked and need not be declared or caught.Option D: True. Once the try
starts, the associated finally
is guaranteed to start (barring VM shutdown), even if a return
occurs inside try
or catch
.
Verification / Alternative check:
Write a small example with returns or thrown exceptions; the finally
still runs, confirming the guarantee.
Why Other Options Are Wrong:
Common Pitfalls:
Believing try
must always have a catch
, or thinking Error
must be declared like IOException
.
Final Answer:
Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
Discussion & Comments