411 REFERENCE · Wikibooks
Introduction to Programming Languages/Finally
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
There is an optional block:finally
Idea Behind finally
The finally block executes ALWAYS.
Even if:
there is an exception;
there is a return;
there is an error.
Main Usage
Resource cleanup.
Example:file.open();
try {
workWith(file);
}
finally {
file.close();
}Without finally:
files may remain open;
connections may leak;
memory may stay allocated.