Home / Questions / What line of a given program will throw FileNotFoundException?
Explanatory Question

What line of a given program will throw FileNotFoundException?

👁 35 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.*;

public class MyReader {
    public static void main(String args[]) {
        try {
            FileReader fileReader = new FileReader("MyFile.java"); // <--- This line
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String strString;
            while ((strString = bufferedReader.readLine()) != null) {
                System.out.println(strString);
            }
            fileReader.close();
        } 
        catch (IOException ie) {
            System.out.println(ie.getMessage());
        }
    }
}

The line FileReader fileReader = new FileReader("MyFile.java"); will throw a FileNotFoundException if "MyFile.java" does not exist.