Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The AutoCloseable interface

Reference for java.lang.AutoCloseable: implement close() so try-with-resources releases your resource, with Closeable differences and suppressed exceptions.

java.lang.AutoCloseable marks an object that holds a resource (a file, a connection, a lock, a temporary state) which must be released when you are done. It has one method, close(), and it is the interface that try-with-resources requires: the statement calls close() for you on every path out of the block.

java.io.Closeable extends it for I/O classes. The difference is small but real: Closeable.close() may throw only IOException and must be idempotent, while AutoCloseable.close() may throw any Exception and is only advised to be idempotent.

Java
public interface AutoCloseable {
    void close() throws Exception;
}

Try it

Java Your own resource

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Exceptions from close() are suppressed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
void close() throws Exception

Returns: nothing. Throws: any Exception; implementations should declare the narrowest type they actually throw, or none.

Related type Description
java.io.Closeable Extends AutoCloseable; close() throws IOException and must be safe to call repeatedly.
Throwable.addSuppressed() / getSuppressed() How try-with-resources attaches failures from close() to the primary exception.
ExecutorService (Java 19+) Implements AutoCloseable; close() waits for submitted tasks to finish.

Usage notes

  • Order. Resources are closed in reverse order of declaration, so a resource declared later (which may depend on an earlier one) is closed first.
  • Primary exception. If the body throws, that exception is the one you catch, and exceptions from close() are added to its suppressed list. If only close() throws, the first such exception is primary and later ones are suppressed.
  • Make close() idempotent. Closing twice should do nothing the second time. try-with-resources calls it once, but manual code may not.
  • Do not swallow InterruptedException. The compiler warns if close() is declared to throw it, because suppressing an interrupt is almost always a bug.
  • Null resources are skipped. try (Foo f = maybeNull()) does not call close() on null and does not throw.
  • Variables must be effectively final when used as try (existingVariable) (Java 9+).
  • Prefer narrow signatures. An implementation that declares close() without throws Exception lets callers skip a catch block, as the first example shows.
  • It is not a destructor. Nothing calls close() unless you do or use try-with-resources; garbage collection never will.