Bird
0
0

Find the error in this Java code snippet:

medium📝 Debug Q6 of 15
Java - Exception Handling
Find the error in this Java code snippet:
try {
int x = 5 / 0;
} catch (Exception e) {
System.out.println("Error");
}
ACatch block should catch ArithmeticException
BDivision by zero causes compile-time error
CMissing finally block causes error
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check for compile-time errors

    Division by zero in Java is a runtime error, so no compile-time error occurs.
  2. Step 2: Verify catch block correctness

    Catch block catches Exception, which includes ArithmeticException, so it is valid.
  3. Final Answer:

    No error, code is correct -> Option D
  4. Quick Check:

    Catch Exception covers all exceptions [OK]
Quick Trick: Catch Exception handles all exceptions including arithmetic [OK]
Common Mistakes:
  • Thinking division by zero causes compile-time error
  • Believing finally block is mandatory
  • Assuming catch must be specific exception

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes