CHAPTER 2 ■ JAVA SYNTAX
Listing 2-14. Handling an exception
try {
averageImpl.setInts(ints2);
} catch(IllegalArgumentException iae) {
System.out.println("Oops! can't use an empty array");
}
That structure is called a try-catch block. As you can see, it's really two blocks—one within the try
statement and one within the catch statement. If the code in the try block throws an exception, the code
in the catch statement gets run. You can have multiple catch statements, each catching a different kind
of exception, within a single try-catch block. That way, you can take different actions for different kinds
of exceptions. You can also add a finally block to a try-catch block. Listing 2-15 shows a more elaborate
example based on the existing example:
Listing 2-15. Try-catch-finally
try {
averageImpl.setInts(ints2);
} catch(IllegalArgumentException iae) {
System.out.println("Oops! can't use an empty array");
} catch(ArithmeticException ae) {
throw ae;
} finally {
System.out.println("Made it past the exception!");
}
Notice that the second catch block re-throws that particular kind of exception. In this case, you
probably wouldn't do that, but the example illustrates a common practice. Exceptions often get re-
thrown, so that they can get handled by the method that makes the most sense for handling that
particular exception. Often, a developer writes a method and has no idea to handle the exception while
writing that method, so the developer re-throws and figures the next method (or the one after that and
so on) will know what to do with it. Of course, if the exception never gets handled, you have a problem
that might crash the program, stick embarrassing exception messages in your output, or otherwise leave
you wiping egg off your face and trying to placate angry users. When you re-throw an exception, the
method containing your try-catch block needs to throw the exception, too. In that fashion, methods can
pass an exception through many methods to get it to a method that knows how to handle it.
The finally keyword lets you create a block of code that gets run no matter what. Even if an
exception happens, the code in the finally block gets run. In the example shown here, it doesn't do
much. finally often isn't included precisely because nothing needs to be done. However, it's sometimes
critically important that a finally block exist to do something. The classic use for a finally block is to
close a database connection. If you open a database connection to do some work but then hit an
exception, the database connection still needs to get closed. Too many open connections slow database
performance and can ultimately crash a database (a disaster for most commercial software).
One last thing to know about exceptions is that Java has two categories of exceptions: checked
exceptions and runtime exceptions. Checked exceptions are called that because the compiler (which is
the program that turns your code into machine code that your computer can understand) checks for
them when you try to compile your code. One big reason to use Eclipse is that it checks for problems as
you write your code. If you use a method that throws checked exceptions, you must handle those
exceptions in your code (though re-throwing them counts as handling them). The compiler does not
check for runtime exceptions. Eclipse runs a compiler for us and checks our code as we type it, so we