DEV Community

Pramod Bablad
Pramod Bablad

Posted on

Java 9 Try With Resources Improvements

Any resource like File or database connection or network connection etc… needs to be released after they are used. Before Java 7, try with finally blocks are used to close the resources.

With the introduction of try-with-resources in Java 7, closing the resources have become even easier. There is no need to explicitly close the resources using finally block. Try-with-resources auto closes the resources used in try block.

But, Java 7 try-with-resources has one drawback. It requires resources to be declared locally within try block. It doesn’t recognize resources declared outside the try block. That issue has been resolved in Java 9.

For examples and how the resources can be handled before Java 7, after Java 7 and after Java 9, see https://javaconceptoftheday.com/java-9-try-with-resources-improvements/ post.

Top comments (0)