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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.