DevNotes
Support
Exception Handling · Chapter 03

try-with-resources

Automatic resource management since Java 7 — AutoCloseable vs Closeable, multiple resources, LIFO closing and the Java 9 form.

◷ 7 min read§ 15 sections? 8 interview questions{ } 24 code examples

#What is try-with-resources?

try-with-resources is a feature introduced in Java 7 to automatically close resources after use.

Before Java 7, developers had to manually close resources inside the finally block. With try-with-resources, the JVM closes resources automatically, making code cleaner, safer and less error-prone.

#Why Was It Introduced?

Consider reading a file:

Java
FileReader reader = new FileReader("data.txt");
BufferedReader br = new BufferedReader(reader);
String line = br.readLine();
System.out.println(line);

After using the file, both resources must be closed. If we forget:

  • File descriptors remain open.
  • Memory leaks may occur.
  • Database connections may not return to the connection pool.
  • Socket connections remain open.

This is known as a resource leak.

Resource Leak Example

Java
public void readFile() throws IOException {
    FileReader reader = new FileReader("data.txt");
    BufferedReader br = new BufferedReader(reader);
    System.out.println(br.readLine());
}
File opened
Never closed
Resource Leak
After many executions: too many open files, rising memory, degraded performance

#Before Java 7

Before try-with-resources, we used finally:

Java
FileReader reader = null;
try {
    reader = new FileReader("data.txt");
} finally {
    if (reader != null) {
        reader.close();
    }
}

Problems:

  • Verbose
  • Easy to forget
  • Nested try-catch
  • Difficult with multiple resources

Multiple Resources Before Java 7

Java
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
    con = getConnection();
    ps = con.prepareStatement(sql);
    rs = ps.executeQuery();
} finally {
    if (rs != null)
        rs.close();
    if (ps != null)
        ps.close();
    if (con != null)
        con.close();
}

Imagine maintaining hundreds of such methods.

#try-with-resources Syntax

Java
try (Resource resource = ...) {
    // Use resource
}

The resource is automatically closed.

Example

Java
try (FileReader reader = new FileReader("data.txt")) {
    // Read file
}

Equivalent to:

Java
FileReader reader = null;
try {
    reader = new FileReader("data.txt");
} finally {
    if (reader != null) {
        reader.close();
    }
}

Real Example

Java
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}

No manual close() required.

#Multiple Resources

Java
try (
    FileReader reader = new FileReader("data.txt");
    BufferedReader br = new BufferedReader(reader)
) {
    System.out.println(br.readLine());
}

Multiple resources are separated using semicolons.

Resource Closing Order

Resources are closed in the reverse order in which they are declared.

Java
try (
    ResourceA a = new ResourceA();
    ResourceB b = new ResourceB();
    ResourceC c = new ResourceC()
) {
}
ResourceC.close()
ResourceB.close()
ResourceA.close()
This is called Last In, First Out (LIFO)

#AutoCloseable Interface

The resource must implement AutoCloseable:

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

When the try block completes, Java automatically invokes resource.close().

Closeable vs AutoCloseable

AutoCloseableCloseable
Introduced in Java 7Older interface (Java 5)
Parent interfaceChild interface
close() throws Exceptionclose() throws IOException
Used by all try-with-resourcesMainly I/O classes
  • AutoCloseable
    • Closeable
      • FileReader
      • BufferedReader
      • InputStream
      • OutputStream

Which Classes Support try-with-resources?

Any class implementing AutoCloseable, for example FileReader, BufferedReader, Scanner, Connection, PreparedStatement, ResultSet, FileInputStream, FileOutputStream, ZipFile, Socket and ServerSocket.

#Custom Resource

You can create your own resource:

Java
class MyResource implements AutoCloseable {

    @Override
    public void close() {
        System.out.println("Closing resource");
    }

    public void work() {
        System.out.println("Working");
    }
}

Usage:

Java
try (MyResource resource = new MyResource()) {
    resource.work();
}
Output
Working
Closing resource

#Java 9 Enhancement

Before Java 9, the resource had to be declared inside the parentheses:

Java
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
}

Java 9 allows an existing variable:

Java
BufferedReader br = new BufferedReader(new FileReader("data.txt"));

try (br) {
    System.out.println(br.readLine());
}

#Execution Flow

Create resource
Enter try block
Execute business logic
Exception?
close() is called automatically either way
Continue execution / exception propagates

What If an Exception Occurs?

Java
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
    throw new RuntimeException("Error");
}
Exception thrown
close()
Exception propagates
Even when an exception occurs, the resource is still closed

try-with-resources with catch

Java
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
    System.out.println(br.readLine());
} catch (IOException e) {
    System.out.println(e.getMessage());
}

Perfectly valid.

try-with-resources with finally

Java
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
    System.out.println(br.readLine());
} catch (IOException e) {
    e.printStackTrace();
} finally {
    System.out.println("Finished");
}
try
close()
catch (if required)
finally
Order — resources are closed before catch and finally run

#Common Mistakes

Forgetting to implement AutoCloseable

Java
class Employee {
}

Cannot be used in try-with-resources. Correct:

Java
class Employee implements AutoCloseable {
    @Override
    public void close() {
    }
}

Calling close() manually

Java
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
    br.close();   // redundant
}

The JVM will close it automatically. For standard I/O classes a second close() is harmless, but the manual call is redundant noise — and for a custom resource whose close() isn't idempotent it can cause problems.

Swallowing Exceptions

Wrong: catch (Exception e) { } — always log or handle exceptions appropriately.

#Enterprise Examples

Reading a configuration file:

Java
try (InputStream input = Files.newInputStream(path)) {
    // Read configuration
}

Reading Excel files using Apache POI:

Java
try (Workbook workbook = WorkbookFactory.create(file)) {
    // Process workbook
}

JDBC:

Java
try (
    Connection con = dataSource.getConnection();
    PreparedStatement ps = con.prepareStatement(sql);
    ResultSet rs = ps.executeQuery()
) {
    while (rs.next()) {
        System.out.println(rs.getString(1));
    }
}

This is the recommended approach in enterprise applications.

#Advantages

  • Automatic resource management
  • Prevents resource leaks
  • Cleaner code
  • Less boilerplate
  • Exception-safe
  • Easier maintenance

#Interview Questions

Q1What is try-with-resources?

A Java 7 feature that automatically closes resources implementing AutoCloseable.

Q2Which interface must a resource implement?

AutoCloseable.

Q3Does try-with-resources replace finally?

Not completely. It replaces finally only for resource cleanup. You may still use finally for other cleanup logic.

Q4Can we have multiple resources?

Yes. Separate them with semicolons.

Q5In what order are resources closed?

Reverse order of declaration (LIFO).

Q6What happens if an exception occurs inside the try block?

The resource is still closed automatically before the exception propagates.

Q7What is the difference between Closeable and AutoCloseable?

Closeable extends AutoCloseable and is specialized for I/O operations. AutoCloseable is more general and is used by the try-with-resources mechanism.

Q8Can custom classes be used?

Yes. Implement the AutoCloseable interface.

#Best Practices

  • Always use try-with-resources for files, streams, sockets and JDBC resources.
  • Avoid manually calling close() inside a try-with-resources block.
  • Keep resources scoped as narrowly as possible.
  • Use multiple resources in a single try block when appropriate.
  • Handle exceptions meaningfully and avoid empty catch blocks.

#Chapter Summary

  • What try-with-resources is and why it was introduced
  • Resource leaks
  • Automatic resource management
  • AutoCloseable and Closeable
  • Multiple resources and LIFO closing order
  • Java 9 enhancement
  • Enterprise JDBC usage
  • Common mistakes, interview questions and best practices