Bird
0
0

Find the problem in this code snippet:

medium📝 Debug Q7 of 15
Java - Exception Handling
Find the problem in this code snippet:
public void openFile() throws IOException {
  try {
    // code
  } catch (Exception e) {
    throw e;
  }
}
AThrowing generic Exception instead of IOException
BMissing finally block
CNo problem, code is correct
DCatching Exception but method declares IOException
Step-by-Step Solution
Solution:
  1. Step 1: Analyze catch block

    Catch block catches Exception, which may include checked exceptions other than IOException.
  2. Step 2: Check method throws declaration

    Method declares only IOException, but rethrows Exception, causing mismatch.
  3. Final Answer:

    Catching Exception but method declares IOException -> Option D
  4. Quick Check:

    Method throws declaration must cover all thrown checked exceptions [OK]
Quick Trick: Throws declaration must match all checked exceptions thrown [OK]
Common Mistakes:
  • Ignoring broader exception types in catch
  • Assuming catch Exception is always safe
  • Missing throws declaration for all checked exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes