Page 607 - Introduction to Programming with Java: A Problem Solving Approach
P. 607
The Exception class’s getMessage method returns a text description of the thrown exception. For example, if you attempt to open a file using a new FileReader(String <filename>) constructor call and you pass in a filename for a file that doesn’t exist, the JVM throws an exception and the getMessage call returns this:
<filename> (The system cannot find the file specified)
The message displays the specified filename where it says <filename>. This message is helpful, but be aware
that sometimes getMessage returns messages that are not particularly helpful. PrintLineFromFile Example
Now let’s look at a complete program example. Figure 14.12’s PrintLineFromFile program opens a user- specified file and prints the file’s first line. The FileReader and BufferedReader constructor calls work in conjunction to open the user-specified file. The BufferedReader’s readLine method reads the first line from the file. The program then prints the line.
Both the FileReader constructor and the readLine method throw checked exceptions, so if we had not put them in a try block, the compiler would have complained and identified the checked excep- tions that needed to be caught. In particular, if the user inputs a filename for a file that doesn’t exist, the FileReader constructor throws a FileNotFoundException. If the file is corrupted and unreadable, the readLine method throws an IOException. Our generic catch block catches either of these ex- ceptions, and we use the getMessage method to print a description of the thrown exception.
In the first sample session in Figure 14.12, the user enters input that tells the program to read the PrintLineFromFile.java source file. Since the program’s first line is a line of *’s, the program prints
Apago PDF Enhancer
a line of *’s.
In the second sample session in Figure 14.12, the user specifies a nonexistent file. The bad file-
name causes the FileReader constructor to throw a FileNotFoundException object, and the getMessage call generates the error message shown.
Now you’ve seen an example of the FileReader constructor throwing an exception, but you haven’t seen an example of the readLine method throwing an exception. That’s because it’s harder to produce an exception with the readLine method call. It occurs only if you have a corrupted file that can be opened, but not read, and we can’t generate such a file intentionally. Even though the readLine method call rarely throws an exception, you must put it inside of a try-catch structure because the compiler knows it might throw a checked exception.
14.10 Multiple catch Blocks
When you have a try block that throws more than one type of exception, you can provide a generic catch block or you can provide a sequence of specific catch blocks—one for each type of exception that might be thrown. Now let’s look at the sequence-of-catch-blocks technique.
PrintLineFromFile Example Revisited
Figure 14.13 shows PrintLineFromFile2, a modified version of the previous PrintLineFromFile program. Instead of using one generic catch block, PrintLineFromFile2 uses a sequence of catch blocks, one catch block for the FileNotFoundException and one catch block for the IOException. Note that because this new program specifically names the types of exceptions that might be thrown, it must im- port their classes. If you run the program with a valid filename for input, you get a printout of the first line
14.10 MultiplecatchBlocks 573