The finally Block
The finally
block always executes when the try
block exits. This ensures that the finally
block is executed even if an unexpected exception occurs. But finally
is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return
, continue
, or break
. Putting cleanup code in a finally
block is always a good practice, even when no exceptions are anticipated.
The try
block of the writeList
method that you’ve been working with here opens a PrintWriter
. The program should close that stream before exiting the writeList
method. This poses a somewhat complicated problem because writeList
‘s try
block can exit in one of three ways.
- The
new FileWriter
statement fails and throws anIOException
. - The
list.get(i)
statement fails and throws anIndexOutOfBoundsException
. - Everything succeeds and the
try
block exits normally.
The runtime system always executes the statements within the finally
block regardless of what happens within the try
block. So it’s the perfect place to perform cleanup.
The following finally
block for the writeList
method cleans up and then closes the PrintWriter
.
finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
} else {
System.out.println("PrintWriter not open");
}
}