Bird
0
0

Consider this code snippet:

hard📝 Application Q9 of 15
Java - Exception Handling
Consider this code snippet:
public void process() throws IOException {
  try {
    throw new IOException("File error");
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

What is the effect of wrapping the checked exception in a RuntimeException?
ASwallows the original exception silently
BCauses compilation error due to throws declaration
CConverts checked exception to unchecked, no need to declare
DForces caller to catch RuntimeException
Step-by-Step Solution
Solution:
  1. Step 1: Analyze exception wrapping

    Wrapping IOException (checked) inside RuntimeException (unchecked) converts it to unchecked.
  2. Step 2: Check throws declaration effect

    Method declares throws IOException but actually throws RuntimeException, which does not require declaration.
  3. Final Answer:

    Converts checked exception to unchecked, no need to declare -> Option C
  4. Quick Check:

    Wrapping checked in unchecked removes declaration requirement [OK]
Quick Trick: Wrap checked exceptions in RuntimeException to avoid throws [OK]
Common Mistakes:
  • Expecting compilation error
  • Thinking original exception is lost
  • Assuming caller must catch RuntimeException

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes