CHAPTER 20

image

Exception Handling

Exception handling allows programmers to deal with unexpected situations that may occur in their programs. As an example, the FileReader class in the java.io package is used to open a file. Creating an instance of this class will cause Netbeans 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.*;// ... FileReader in = new FileReader("Missing.file"); // error

Try-catch

To get rid of this compile-time error the exception must be caught by using a try-catch statement. This statement consists ...

Get Java Quick Syntax Reference now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.