Bird
0
0

How can you handle both IOException and FileNotFoundException in multiple catch blocks correctly?

hard📝 Application Q8 of 15
Java - Exception Handling

How can you handle both IOException and FileNotFoundException in multiple catch blocks correctly?

ACatch only <code>IOException</code>
BCatch <code>FileNotFoundException</code> first, then <code>IOException</code>
CCatch <code>IOException</code> first, then <code>FileNotFoundException</code>
DCatch only <code>FileNotFoundException</code>
Step-by-Step Solution
Solution:
  1. Step 1: Understand exception hierarchy

    FileNotFoundException is subclass of IOException.
  2. Step 2: Order catch blocks

    Subclass exception must be caught before superclass to avoid unreachable code.
  3. Final Answer:

    Catch FileNotFoundException first, then IOException -> Option B
  4. Quick Check:

    Subclass catch before superclass catch [OK]
Quick Trick: Catch subclass exceptions before superclass exceptions [OK]
Common Mistakes:
  • Catching superclass before subclass causes unreachable catch
  • Ignoring exception inheritance
  • Catching only one exception when both can occur

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes