Prefer try-with-resources to try-finally

The Java libraries include many resources that must be closed manually by invoking a close method. Examples include InputStream, OutputStream, and java.sql.Connection.  While many of these resources use finalizers as a safety net, finalizers don’t work very well.  For a long time, a try-finally statement was the best way to guarantee that a resource would be closed properly, even in the face … More Prefer try-with-resources to try-finally