Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Java - Exception Handling

What will be the output of this code?

try {
  int a = 5 / 0;
} catch (ArithmeticException e) {
  System.out.println("Arithmetic Exception caught");
} catch (Exception e) {
  System.out.println("General Exception caught");
}
ANo output
BArithmetic Exception caught
CGeneral Exception caught
DCompilation error
Step-by-Step Solution
Solution:
  1. Step 1: Identify exception thrown

    Division by zero throws ArithmeticException.
  2. Step 2: Determine which catch block handles it

    The first catch block matches ArithmeticException, so it runs.
  3. Final Answer:

    Arithmetic Exception caught -> Option B
  4. Quick Check:

    Specific catch handles matching exception first [OK]
Quick Trick: Specific exceptions caught before general ones [OK]
Common Mistakes:
  • Assuming general Exception catch runs first
  • Thinking division by zero causes compile error
  • Ignoring exception type matching

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes