The finally block always executes after a try block, regardless of how the control exits the block.
The finally block always executes after a try block, regardless of how the control exits the block. This includes whether:
- The try block exits normally
- The try block throws an exception caught by a local catch block
- The try block throws an exception not caught by a local catch block.
- The catch block exits normally.
- The catch block itself throws an exception.
- Any other statements leaving the block (e.g.,
goto
,break
,return
).
It is useful for including commands which must be executed before exiting a code block. Often, it is used in connection with resource management.