๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

Java Try with Resources

Try with resources opens a resource in the try header and closes it automatically. The close happens when the block ends, on every path out.

Closed without a close call

The Tray class prints from its close method. No line of the program calls close.

public class Main {
  public static void main(String[] args) {
    try (Tray tray = new Tray("tray")) {
      System.out.println("using tray");
    }
  }
}

class Tray implements AutoCloseable {
  private String name;

  Tray(String name) {
    this.name = name;
  }

  public void close() {
    System.out.println("closed " + name);
  }
}

Closed before the catch runs

The block below throws after its use line. The closed line still prints, and it prints before the catch line.

public class Main {
  public static void main(String[] args) {
    try (Tray tray = new Tray("tray")) {
      System.out.println("using tray");
      throw new IllegalStateException("the tray jammed");
    } catch (IllegalStateException e) {
      System.out.println("caught: " + e.getMessage());
    }
  }
}

class Tray implements AutoCloseable {
  private String name;

  Tray(String name) {
    this.name = name;
  }

  public void close() {
    System.out.println("closed " + name);
  }
}

Two resources close in reverse

Two trays open in the order first, second. They close in the opposite order.

public class Main {
  public static void main(String[] args) {
    try (Tray first = new Tray("first"); Tray second = new Tray("second")) {
      System.out.println("using both");
    }
  }
}

class Tray implements AutoCloseable {
  private String name;

  Tray(String name) {
    this.name = name;
  }

  public void close() {
    System.out.println("closed " + name);
  }
}

Example

public class Main {
  public static void main(String[] args) {
    try (Tray small = new Tray("small"); Tray large = new Tray("large")) {
      System.out.println("serving lunch");
      throw new IllegalStateException("the kitchen closed");
    } catch (IllegalStateException e) {
      System.out.println("caught: " + e.getMessage());
    }
  }
}

class Tray implements AutoCloseable {
  private String name;

  Tray(String name) {
    this.name = name;
  }

  public void close() {
    System.out.println("closed " + name);
  }
}

Key points

  • A resource class implements AutoCloseable and defines a close method.
  • The resource is declared inside the parentheses of the try header.
  • No close call is written in the code, since the block closes the resource itself.
  • The close runs even when the block throws an exception.
  • Several resources in one header close in reverse opening order.
  • The form guarantees the close on normal exits and on exception exits alike.

In this example, two trays serve lunch, the block throws, both trays close in reverse order, and the catch line prints last.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

When does try with resources close its resource?