In this article, we are going to discuss about the improvement made in ‘Try-With-Resources’ feature in the Java 9 Release. But, in order to understand the change being made, we need to know first what actually ‘try-with-resources’ feature is. So, let’s go ahead and learn all of that in this post with examples.

Try_With_Resources_Enhancement_Java9_Featured_Image_Techndeck

Check out: Iterate / ofNullable operations in Java 9 Stream

Try-With-Resources Improvement in Java 9...!!! Share on X

In this article, we are going to cover below points:

  1. What is ‘Try-With-Resources’ feature in Java 7?
  2. Limitation with this feature in Java 7?
  3. How Java 9 solves that problem?

Let’s begin:

1. What is ‘Try-with-Resource’ feature in Java 7?

Try-with-Resource was introduced in Java 7 and it’s purpose is to close the resource(s) automatically after being used. Let’s say you are using any object implementing java.lang.AutoCloseable or java.io.Closeable interface like File object OR Database object . Now, the objective was to close such resources once their usage is done. Before Java 7, you had to explicitly close such objects in the finally block. But, Java 7 helps to auto-close such resources once used.

Great Huh….

2. Is there any limitation with this feature in Java 7, if yes, what is it?

Yes. There is a limitation here. Challenge is that the resource need to be declared before try or inside the try statement else it will throw the compilation error. See examples below:

Below code will work in Java 7: (because object is declared within try statement)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class Try_With_Resouces_Java7_Example {

    public static void main(String[] args) {

        String line;

        Reader inputStringReaderObj = new StringReader("Techndeck.com");

        BufferedReader brObj = new BufferedReader(inputStringReaderObj);

        try (BufferedReader brObj1 = brObj) {

            while ((line = brObj1.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Below code won’t work in Java 7: (because object is not declared within try statement)

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {

        String line;

        Reader inputStringReaderObj = new StringReader("Techndeck.com");

        BufferedReader brObj = new BufferedReader(inputStringReaderObj);

        try (brObj) {

            while ((line = brObj.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Java 7 Error message:

Main.java:17: error:  expected
        try(brObj) {
                 ^
Main.java:17: error: ')' expected
        try(brObj) {
                  ^
Main.java:17: error: '{' expected
        try(brObj) {
                    ^
Main.java:19: error: not a statement
            while ((line = brObj.readLine()) != null) {
                  ^
Main.java:19: error: ';' expected
            while ((line = brObj.readLine()) != null) {
                                                     ^
5 errors

3.  How Java 9 handle this limitation?

Well, Java 9 improved ‘Try-With-Resources’ and it is no longer needed to declare the object inside the try statement. Lets run the above  example(Main.java) in Java 9 now.

It will work completely fine and will generate the below output.

Java 9 Output:

Techndeck.com

Try-With-Resources Improvement in Java 9...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts