Home / Questions / Example: IOException
Explanatory Question

Example: IOException

👁 26 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation


import java.io.FileReader;
import java.io.IOException;

public class CheckedExample {
    public static void main(String[] args) {
        try {
            FileReader file = new FileReader("non_existing_file.txt");
            file.read();
            file.close();
        } catch (IOException e) {
            System.out.println("File not found or cannot be read!");
        }
    }
}

📘 Explanation:
The compiler forces you to handle IOException because the file may not exist or may not be readable.