Order of catch
Blocks for IOException
and FileNotFoundException
In Java, exception handling follows the rule that subclass exceptions must be caught before their superclass exceptions.
✅ Key Rule:
FileNotFoundException
is a subclass ofIOException
, so it must be caught first.- If
IOException
is caught first, it will catch allFileNotFoundException
instances as well, making theFileNotFoundException
catch block unreachable, causing a compilation error.
1. Correct Order of catch
Blocks
import java.io.*;
public class ExceptionOrderExample {
public static void main(String[] args) {
try {
throw new FileNotFoundException("File not found error!");
} catch (FileNotFoundException e) { // ✅ Subclass exception must be caught first
System.out.println("Caught FileNotFoundException: " + e.getMessage());
} catch (IOException e) { // ✅ Superclass exception is caught after
System.out.println("Caught IOException: " + e.getMessage());
}
}
}
✅ Output:
Caught FileNotFoundException: File not found error!
🔹 Key Takeaway: FileNotFoundException
is caught first, and no other catch
block executes.
2. Incorrect Order (Compilation Error)
🚨 If IOException
is caught first, the FileNotFoundException
block becomes unreachable:
try {
throw new FileNotFoundException("File not found error!");
} catch (IOException e) { // ❌ This catches FileNotFoundException too!
System.out.println("Caught IOException: " + e.getMessage());
} catch (FileNotFoundException e) { // ❌ Unreachable code! Compilation error
System.out.println("Caught FileNotFoundException: " + e.getMessage());
}
🛑 Compilation Error:
ExceptionOrderExample.java:7: error: exception FileNotFoundException has already been caught
🔹 Reason: IOException
is a superclass of FileNotFoundException
, so it already catches all FileNotFoundException
instances.
3. How Many catch
Blocks Will Execute?
✅ Only One catch
Block Executes
- When an exception occurs, Java starts checking
catch
blocks from top to bottom. - Only the first matching
catch
block executes. - The remaining
catch
blocks are skipped.
Example: Which Catch Block Will Execute?
try {
throw new FileNotFoundException("File not found error!");
} catch (FileNotFoundException e) {
System.out.println("Caught FileNotFoundException");
} catch (IOException e) {
System.out.println("Caught IOException");
} catch (Exception e) {
System.out.println("Caught Exception");
}
✅ Output:
Caught FileNotFoundException
🔹 Key Takeaway: The first matching catch
block executes, and the rest are ignored.
4. What If an IOException
Is Thrown Instead?
If we throw an IOException
instead of FileNotFoundException
, the IOException
catch block executes:
try {
throw new IOException("General IO error!");
} catch (FileNotFoundException e) {
System.out.println("Caught FileNotFoundException");
} catch (IOException e) {
System.out.println("Caught IOException");
}
✅ Output:
Caught IOException
🔹 Key Takeaway: Since the exception is not FileNotFoundException
, Java moves to the next matching catch
block (IOException
).
5. Summary
Scenario | Which catch Block Executes? |
---|---|
FileNotFoundException is thrown | ✅ FileNotFoundException block |
IOException is thrown | ✅ IOException block |
Superclass (IOException ) is caught first | 🚨 Compilation error |
Final Thoughts
✔ Always catch the subclass (FileNotFoundException
) before its superclass (IOException
).
✔ Only one catch
block executes for a given exception.
✔ If IOException
is caught first, FileNotFoundException
becomes unreachable (compilation error).