A finally
block is much safer, as it's guaranteed to execute even if your try
block throws an exception that isn't handled by the catch
block or return
s.
It also has the side effect of organizing all the "cleanup" code in an expected location, making the code (arguably) easier to maintain.
It's worth noting, by the way, that if you're using Java 7 or higher, and the resource you're handling is an AutoCloseable
(such as a java.sql.Connection
, for example), there's an even cleaner solution - the try-with-resource syntax, which saves you the hassle of explicitly closing the resource:
try (Connection con = /* something */) {
// code
} catch (SomeException e) {
// handle
// If there's nothing intelligent to do, this entire clause can be omitted.
}