Page 610 - Introduction to Programming with Java: A Problem Solving Approach
P. 610
576 Chapter 14 Exception Handling
of that file, just as before. But if you supply an invalid filename for input, you get something like the sample
session at the bottom of Figure 14.13.
catch Block Ordering—The Order Matters
If there are multiple catch blocks, the first catch block that matches the type of the exception thrown is the one that’s executed. Then the other catch blocks are skipped. This behavior is similar to the behavior of a switch statement. But there is a slight difference. With a switch statement, after a matching case block is found and executed, control continues to the next case unless there happens to be a break state- ment. With catch blocks, after a matching catch block is found and executed, the subsequent catch blocks are automatically skipped.
Whenever you use more than one catch block after a given try block, and one catch block’s excep- tion class is derived from another catch block’s exception class, you must arrange the catch blocks with the more general exception classes at the bottom. For example, if you look up the FileNotFoundException on Sun’s Java API Web site, you’ll see this hierarchy:
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.io.IOException
java.io.FileNotFoundException
If you choose to have a FileNotFoundException catch block and an IOException catch Apago PDF Enhancer
block in the same catch-block sequence, then you must put the IOException catch block at the bot- tom because the IOException class is a more general version of the FileNotFoundException class. If you put the IOException catch block first, it would match both types of exceptions, and the FileNotFoundException catch block would always be skipped. And that’s not good. As long as you understand this principle, there’s no need to memorize the hierarchical relationships among all types of exceptions, because the compiler will tell you in a compile-time error if you arrange multiple catch blocks in the wrong order.
Generic catch Block Versus Multiple catch Blocks
In the previous section, we looked at the generic-catch-block technique. In this section we looked at the sequence-of-catch-blocks technique. Which one is better? The generic-catch-block technique is slightly easier to code, so if you’re interested in simplicity, use that technique. The sequence-of-catch-blocks tech- nique allows you to handle different exceptions differently, so if you’re interested in having more control over your exception handling and more control over your error messages, use that technique.
14.11 Understanding Exception Messages
Unless you’re incredibly careful, you’ve probably written programs that have generated runtime error mes- sages. But prior to this chapter, you weren’t properly prepared to thoroughly understand those error mes- sages. Now you are. In this section, we describe exception messages by showing exception message details in the context of a complete program.