4 Java Try With Resource Tips You Need To Learn Now

What is Java try with resource?

The java try with resource statement is a try statement that declares one or more resources and closes them automatically. We dont have to close them explicitly. A resource is an object that must be closed after the program is finished with it. The try with resources statement ensures that each resource is closed at the end of the statement. We can only use the resources in java try with resources which implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Lets checkout our previous program and run it.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

public class TryCatchFinallyExample {

  static List<String> list = null;

  public static void main(String[] args) throws IOException {
    System.out.println("Reading a sample file");
    BufferedReader br = null;
    try {
      File file = new File("sample.txt");
       br = new BufferedReader(new FileReader(file));
      System.out.println(br.readLine());
      list.add(br.readLine());

    } catch (Exception e) {
      System.out.println("Something went wrong...");
      e.printStackTrace();
    } finally {
      System.out.println("I will always execute and thats why I am closing all opened resources");
      br.close();
    }
  }
}

In this program we are explicitly closing the BufferedReader in finally block. Prior to the Java 7 we have to close the resources explicitly but Java 7 introduced the new try with resources feature where in java takes care of closing the resources automatically.

In try with resource we have defined BufferedReader object and passed as parameter to the try block as below. This will take care of closing that BufferedReader implicitly.

try(BufferedReader br = new BufferedReader(new FileReader("sample.txt"));) {

Lets modify our code and add the java try with resource in above code.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

public class TryCatchFinallyExample {

  static List<String> list = null;

  public static void main(String[] args) {
    System.out.println("Reading a sample file");
    try(BufferedReader br = new BufferedReader(new FileReader("sample.txt"));) {
      System.out.println(br.readLine());
      list.add(br.readLine());
    } catch (Exception e) {
      System.out.println("Something went wrong...");
      e.printStackTrace();
    }
  }
}

4 Java Try With Resource Tips You Need To Learn Now

  1. You can only use the resources which implements AutoCloseable interface.
  2. Always use try with resources whenever you are using resource(opening a file / opening a database connection) in your code.
  3. Initialise and declare those resources in parameterised try block
  4. try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.