The finally keyword is used to define a block that is always executed in a try-catch-finally statement.
A finally block typically contains cleanup code that recovers from partial execution of a try block.
try
{
<block that may throw exceptions>
}
catch (<java.lang.Exception or subclass> e)
{
<code to handle exception e>
}
finally
{
<statements that execute with or without exception>
}
The opening and closing curly braces { and } are part of the syntax of the finally clause and may not be omitted even if the clause contains a single statement.
Every try block must have at least one catch or finally clause.
If any portion of the try block is executed, the code in a finally block is always guaranteed to be executed whether an exception occurs or not and independent of whether the try or catch blocks contain return, continue or break statements.
In the absence of exceptions, control flows through the try block and then into the finally block.
If an exception occurs during execution of the try block and the appropriate catch block contains a break, continue or return statement, control flows through the finally block before the break, continue or return occurs.