Java Reference
In-Depth Information
present, it's placed after the last catch block. If there are no catch blocks, the finally
block, if present, immediately follows the try block.
When the finally Block Executes
The finally block will execute whether or not an exception is thrown in the corresponding
try block. The finally block also will execute if a try block exits by using a return ,
break or continue statement or simply by reaching its closing right brace. The one case
in which the finally block will not execute is if the application exits early from a try block
by calling method System.exit . This method, which we demonstrate in Chapter 15, im-
mediately terminates an application.
If an exception that occurs in a try block cannot be caught by one of that try block's
catch handlers, the program skips the rest of the try block and control proceeds to the
finally block. Then the program passes the exception to the next outer try block—nor-
mally in the calling method—where an associated catch block might catch it. This process
can occur through many levels of try blocks. Also, the exception could go uncaught (as we
discussed in Section 11.3.
If a catch block throws an exception, the finally block still executes. Then the
exception is passed to the next outer try block—again, normally in the calling method.
Releasing Resources in a finally Block
Because a finally block always executes, it typically contains resource-release code . Suppose
a resource is allocated in a try block. If no exception occurs, the catch blocks are skipped and
control proceeds to the finally block, which frees the resource. Control then proceeds to
the first statement after the finally block. If an exception occurs in the try block, the try
block terminates . If the program catches the exception in one of the corresponding catch
blocks, it processes the exception, then the finally block releases the resource and control
proceeds to the first statement after the finally block. If the program doesn't catch the ex-
ception, the finally block still releases the resource and an attempt is made to catch the ex-
ception in a calling method.
Error-Prevention Tip 11.5
The finally block is an ideal place to release resources acquired in a try block (such as
opened files), which helps eliminate resource leaks.
Performance Tip 11.1
Always release a resource explicitly and at the earliest possible moment at which it's no lon-
ger needed. This makes resources available for reuse as early as possible, thus improving
resource utilization and program performance.
Demonstrating the finally Block
Figure 11.5 demonstrates that the finally block executes even if an exception is not
thrown in the corresponding try block. The program contains static methods main
(lines 6-18), throwException (lines 21-44) and doesNotThrowException (lines 47-64).
Methods throwException and doesNotThrowException are declared static , so main can
call them directly without instantiating a UsingExceptions object.
 
Search WWH ::




Custom Search