Java exceptions — select the true statement about try/catch/finally behavior and declaration requirements.

Java Programming Exceptions Difficulty: Easy
Choose an option
  • A
    A try statement must have at least one corresponding catch block.
  • B
    Multiple catch statements can catch the same class of exception more than once.
  • C
    An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.
  • D
    Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.

Answer

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:

  • We consider standard Java SE behavior (no suppressed VM bugs).
  • “VM shutdown” includes System.exit, power loss, process kill, or similar hard-termination scenarios.

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:

Option A: False. Java allows 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:

  • They contradict Java’s specification for checked vs unchecked exceptions, reachability, and try/finally requirements.

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
No comments yet. Be the first to comment!
Join Discussion