Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
Java - Exception Handling

Identify the error in the following code snippet:

try {
    int a = 5 / 0;
} finally {
    System.out.println("Cleanup");
}
ANo error, code is valid
Bfinally block cannot be used without catch
CSyntax error in try block
DMissing catch block for exception
Step-by-Step Solution
Solution:
  1. Step 1: Check try-finally syntax rules

    Java allows try-finally without catch; try must be followed by catch or/and finally.
  2. Step 2: Analyze exception handling

    Division by zero throws ArithmeticException at runtime, finally executes cleanup, then exception propagates to caller.
  3. Step 3: Confirm no error

    The code compiles and runs validly (prints "Cleanup" before propagating exception); no syntax or structural error.
  4. Final Answer:

    No error, code is valid -> Option A
  5. Quick Check:

    try-finally valid without catch = D [OK]
Quick Trick: try-finally without catch is valid [OK]
Common Mistakes:
  • Thinking finally requires catch block
  • Confusing runtime exception with syntax error
  • Believing finally alone causes compile error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes