The
finally
block always executes when thetry
block exits. This ensures that thefinally
block is executed even if an unexpected exception occurs. Butfinally
is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by areturn
,continue
, orbreak
. Putting cleanup code in afinally
block is always a good practice, even when no exceptions are anticipated.
https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html