Bird
0
0

Identify the error in the following code:

medium📝 Debug Q6 of 15
Java - Exception Handling
Identify the error in the following code:
public void process() {
  try {
    throw new Exception();
  } catch (RuntimeException e) {
    System.out.println("Caught");
  }
}
ANo error
BCatch block does not catch Exception
CTry block missing
DThrows declaration missing
Step-by-Step Solution
Solution:
  1. Step 1: Check exception thrown

    Method throws Exception, a checked exception, inside try block.
  2. Step 2: Analyze catch block

    Catch block catches RuntimeException, which does not catch Exception.
  3. Final Answer:

    Catch block does not catch Exception -> Option B
  4. Quick Check:

    Catch type must match or be superclass of thrown exception [OK]
Quick Trick: Catch block must match thrown exception type [OK]
Common Mistakes:
  • Assuming catch catches all exceptions
  • Ignoring checked vs unchecked distinction
  • Missing throws declaration for checked exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes