Exception handling allows programmers to deal with unexpected situations that may occur in their programs. For example, the
FileReader class in the java.io package is used to open a file. Creating an instance of this class will cause the IDE to give a reminder that the class’s constructor may throw a
FileNotFoundException. Attempting to run the program will also cause the compiler to point this out.
import java.io.*;
public class MyClass
{
public static void main(String[] args)
{
// Compile-time error
FileReader file = new FileReader("missing.txt");
}
}