Java ProgramsExceptionsIOException

IOException in Java

intermediate·  Exceptions  ·  Error Handling

Problem

FileNotFoundException is a subtype of IOException, so a catch block written for the broader IOException still catches a missing-file failure — it just doesn't distinguish it from any other kind of I/O error.

Attempt an I/O operation that can fail in more than one way, and handle any failure through a single general catch.

Input
new FileReader("missing-file.txt")
Output
Error: Could not open file - FileNotFoundException

Java Program

Java
import java.io.FileReader; import java.io.IOException; public class IOExceptionExample { public static void main(String[] args) { try { FileReader reader = new FileReader("missing-file.txt"); } catch (IOException e) { // getSimpleName() shows the specific subtype the general catch still caught System.out.println("Error: Could not open file - " + e.getClass().getSimpleName()); } } }

Output

Error: Could not open file - FileNotFoundException

Core Logic

Catching IOException instead of FileNotFoundException specifically still handles the same missing-file failure, because FileNotFoundException is-a IOException — the catch just can't tell which specific subtype occurred without asking.

How It Works
  1. 1new FileReader("missing-file.txt") throws a FileNotFoundException when the file doesn't exist, exactly as before.
  2. 2catch (IOException e) matches it anyway, since FileNotFoundException extends IOException.
  3. 3e.getClass().getSimpleName() reveals the exception's actual runtime type, even though the catch clause only named its supertype.
  4. 4A single IOException catch would just as readily catch a different I/O failure — a read error, a permissions problem — without needing a separate catch block for each one.
The same missing-file failure occurs, but this time it's caught by the broader IOException clause, and the printed class name still shows FileNotFoundException underneath.
💡

Key Point: Reach for the specific subtype (FileNotFoundException) when the response genuinely differs by failure type; reach for the supertype (IOException) when one recovery path is good enough for any I/O failure that could occur — this file and FileNotFoundException.mdx demonstrate exactly that choice on the identical scenario.

Key Concepts

FileReaderexception supertypetry / catch

Related Programs