finally
A finally
block may also be specified after or
instead of catch
blocks. Code within the finally
block will always be
executed after the try
and catch
blocks, regardless of whether an
exception has been thrown, and before normal execution resumes.
One notable interaction is between the finally
block and a return
statement.
If a return
statement is encountered inside either the try
or the catch
blocks,
the finally
block will still be executed. Moreover, the return
statement is
evaluated when encountered, but the result will be returned after the finally
block
is executed. Additionally, if the finally
block also contains a return
statement,
the value from the finally
block is returned.
Another notable interaction is between an exception thrown from within a try
block,
and an exception thrown from within a finally
block. If both blocks throw an exception,
then the exception thrown from the finally
block will be the one that is propagated,
and the exception thrown from the try
block will used as its previous exception.