7.6 Performing cleanup with finally



There's often some piece of code that you want to execute whether or not an exception occurs in a try block.

Cae Study: FinallyWorks.java

Whether an exception is thrown or not, the finally clause is always executed.
 

7.6.1 What's finally for?

finally is necessary when you need to set something other than memory back to its original state.

Case Study: OnOffSwitch.java, WithFinally.java

Even in cases in which the exception is not caught in the current set of catch clauses, finally will be executed before the exception-handling mechanism continues its search for a handler at the next higher level.

Case Study: AlwaysFinally.java
 

7.6.2 Pitfall: the lost exception

Case Study: LostMessage.java

Next Page