Java Keyword: finally

The finally keyword is used to open a block of code that executes after the preceeding try block, regardless of whether an exception was thrown or not.

try {
    // code that may throw an exception
} finally {
    // executed regardless if the above
    // code threw an exception or not
}

A finally block is often used to restore state, for instance by releasing resources aquired in the preceeding try block.

It can also be used together with catch blocks and try-with-resources. See Java: try + finally for details and examples.

Comments

Be the first to comment!