Previous Table of Contents Next


14.2.12.7. The package and import Statements

The package statement, as we saw earlier in the section, specifies the package that the classes in a file of Java source code are part of. If it appears, it must be the first statement of a Java file. The import statement, which we also saw earlier, allows us to refer to classes by abbreviated names. import statements must appear after the package statement, if any, and before any other statements in a Java file. For example:

   package games.tetris;

   import java.applet.*;
   import java.awt.*;

14.2.13. Exceptions and Exception Handling

Exception handing is a significant new feature of Java.9 There are a number of new terms associated with exception handling. First, an exception is a signal that indicates that some sort of exceptional condition (such as an error) has occurred. To throw an exception is to signal an exceptional condition. To catch an exception is to handle it—to take whatever actions are necessary to recover from it.


9It is similar to, but not quite the same as, exception handling in C++.

Exceptions propagate up through the lexical block structure of a Java method, and then up the method call stack. If an exception is not caught by the block of code that throws it, it propagates to the next higher enclosing block of code. If it is not caught there, it propagates up again. If it is not caught anywhere in the method, it propagates to the invoking method, where it again propagates through the block structure. If an exception is never caught, it propagates all the way to the main() method from which the program started, and causes the Java interpreter to print an error message and a stack trace and exit.

As we’ll see in the subsections below, exceptions make error handling (and “exceptional condition” handling) more regular and logical by allowing you to group all your exception handling code into one place. Instead of worrying about all of the things that can go wrong with each line of your code, you can concentrate on the algorithm at hand and place all your error handling code (that is, your exception catching code) in a single place.

14.2.13.1. Exception Objects

An exception in Java is an object that is an instance of some subclass of java.lang.Throwable. Throwable has two standard subclasses: java.lang.Error and java.lang.Exception.10 Exceptions that are subclasses of Error generally indicate linkage problems related to dynamic loading, or virtual machine problems such as running out of memory. They should almost always be considered unrecoverable, and should not be caught. While the distinction is not always clear, exceptions that are subclasses of Exception indicate conditions that may be caught and recovered from. They include such exceptions as java.io.EOFException, which signals the end of a file and java.lang.ArrayAccessOutOfBounds, which indicates that a program has tried to read past the end of an array.


10We’ll use the term “exception” to refer to any subclass of Throwable, whether it is actually an Exception or an Error.

Since exceptions are objects, they can contain data and define methods. The Throwable object, at the top of the exception class hierarchy, includes a String message in its definition and this field is inherited by all exception classes. This field is used to store a human-readable error message that describes the exceptional condition. It is set when the exception object is created by passing an argument to the constructor method. The message can be read from the exception with the Throwable.get.Message() method. Most exceptions contain only this single message, but a few add other data. The java.io.InterruptedIOException, for example, adds the following field:

   public int bytesTransferred;

This field specifies how much of the I/O was complete before the exceptional condition occurred.

14.2.13.2. Exception Handling

The try/catch/finally statement is Java’s exception handling mechanism. try establishes a block of code that is to have its exceptions and abnormal exits (through break, continue, return, or exception propagation) handled. The try block is followed by zero or more catch clauses that catch and handle specified types of exceptions. The catch clauses are optionally followed by a finally block that contains “cleanup” code. The statements of a finally block are guaranteed to be executed, regardless of how the code in the try block exits. A detailed example of the try/catch/finally syntax is shown in Example 14.5.

EXAMPLE 14.5. The try/catch/finally statement.

   try {
           // Normally this code runs from the top of the block to the
           // bottom without problems. But it sometimes may raise
           // exceptions or exit the block via a break, continue,
           // or return statement.
}
   catch (SomeException e1) {
           // Handle an exception object e1 of type SomeException
           // or of a subclass of that type.
}
   catch (AnotherException e2) {
           // Handle an exception object e2 of type AnotherException
           // or of a subclass of that type.
   }
   finally {
           // Always execute this code, after we leave the try clause,
           // regardless of whether we leave it:
           //    1) Normally, after reaching the bottom of the block.
           //    2) With an exception that is handled by a catch.
           //    3) With an exception that is not handled.
           //    4) Because of a break, continue, or return statement.
   }

14.2.13.3. try

The try clause simply establishes a block of code that is to have its exceptions and abnormal exits (through break, continue, return, or exception propagation) handled. The try clause by itself doesn’t do anything interesting; it is the catch and finally clauses that do the exception handling and clean-up operations.


Previous Table of Contents Next