Java Reference
In-Depth Information
Using finally
Sometimes you will want to define a block of code that will execute when a
try/catch
block
is left. For example, an exception might cause an error that terminates the current method,
causing its premature return. However, that method may have opened a file or a network
connection that needs to be closed. Such types of circumstances are common in program-
ming, and Java provides a convenient way to handle them:
finally
.
To specify a block of code to execute when a
try/catch
block is exited, include a
finally
block at the end of a
try
/
catch
sequence. The general form of a
try
/
catch
that includes
fi-
nally
is shown here.