Java Language Reference

Previous Chapter 9 Next
 

9. Exception Handling

Contents:
Handling Exceptions
Declaring Exceptions
Generating Exceptions
The Exception Hierarchy

Exception handling is a mechanism that allows Java programs to handle various exceptional conditions, such as semantic violations of the language and program-defined errors, in a robust way. When an exceptional condition occurs, an exception is thrown. If the Java virtual machine or run-time environment detects a semantic violation, the virtual machine or run-time environment implicitly throws an exception. Alternately, a program can throw an exception explicitly using the throw statement. After an exception is thrown, control is transferred from the current point of execution to an appropriate catch clause of an enclosing try statement. The catch clause is called an exception handler because it handles the exception by taking whatever actions are necessary to recover from it.

9.1 Handling Exceptions

The try statement provides Java's exception-handling mechanism. A try statement contains a block of code to be executed. Putting a block in a try statement indicates that any exceptions or other abnormal exits in the block are going to be handled appropriately. A try statement can have any number of optional catch clauses that act as exception handlers for the try block. A try statement can also have a finally clause. The finally block is always executed before control leaves the try statement; it cleans up after the try block. Note that a try statement must have either a catch clause or a finally clause.

Here is an example of a try statement that includes a catch clause and a finally clause:

try {
    out.write(b);
} catch (IOException e) {
    System.out.println("Output Error");
} finally {
    out.close();
}

If out.write() throws an IOException, the exception is caught by the catch clause. Regardless of whether out.write() returns normally or throws an exception, the finally block is executed, which ensures that out.close() is always called.

A try statement executes the block that follows the keyword try. If an exception is thrown from within the try block and the try statement has any catch clauses, those clauses are searched, in order, for one that can handle the exception. If a catch clause handles an exception, that catch block is executed.

However, if the try statement does not have any catch clauses that can handle the exception (or does not have any catch clauses at all), the exception propagates up through enclosing statements in the current method. If the current method does not contain a try statement that can handle the exception, the exception propagates up to the invoking method. If this method does not contain an appropriate try statement, the exception propagates up again, and so on. Finally, if no try statement is found to handle the exception, the currently running thread terminates.

A catch clause is declared with a parameter that specifies the type of exception it can handle. The parameter in a catch clause must be of type Throwable or one of its subclasses. When an exception occurs, the catch clauses are searched for the first one with a parameter that matches the type of the exception thrown or is a superclass of the thrown exception. When the appropriate catch block is executed, the actual exception object is passed as an argument to the catch block. The code within a catch block should do whatever is necessary to handle the exceptional condition.

The finally clause of a try statement is always executed, no matter how control leaves the try statement. Thus it is a good place to handle clean-up operations, such as closing files, freeing resources, and closing network connections.

References The throw Statement; The try Statement; Throwable


Previous Home Next
Synchronizing Multiple Threads Book Index Declaring Exceptions

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java
This HTML Help has been published using the chm2web software.