Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q6 of 15
Java - Exception Handling

Identify the error in the following code snippet:

public void write() throws IOException {
    // code
}
public void save() {
    write();
}
AMethod save() must declare or handle IOException
BMethod write() cannot throw IOException
CNo error, code is correct
DMethod save() should throw RuntimeException
Step-by-Step Solution
Solution:
  1. Step 1: Check exception declaration in write()

    The method write() declares it throws IOException, a checked exception.
  2. Step 2: Check save() method's handling

    The method save() calls write() but neither handles nor declares the exception.
  3. Final Answer:

    Method save() must declare or handle IOException -> Option A
  4. Quick Check:

    Checked exceptions must be handled or declared by caller [OK]
Quick Trick: Caller must declare or catch checked exceptions [OK]
Common Mistakes:
  • Ignoring exception declaration in caller
  • Thinking IOException is unchecked
  • Assuming code compiles without handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes