BackendBench
Coffee

Unravel › Java concepts › Errors and logging

Errors and logging

try-with-resources

try-with-resources opens a resource in the parentheses after try and closes it automatically when the block ends, normally or by exception, so files, connections and streams are never leaked.

Learn these first

At a glance

Mental model

A self-closing door: however you leave the room, it shuts behind you.

What happens, in order
  1. 1try (var in = Files.newBufferedReader(path)) { ..} calls in.close() when the block is left. Anything that implements AutoCloseable works.
  2. 2Several resources are closed in reverse order of opening: the last opened is closed first
  3. 3If the body throws and close() also throws, the body's exception wins and the close failure is attached as a suppressed exception
Remember
Watch out
Opening a resource outside the parentheses and forgetting to close itIgnoring suppressed exceptions when debugging
Example (highlighted lines explain it)
try (BufferedReader in = Files.newBufferedReader(path);     BufferedWriter out = Files.newBufferedWriter(target)) {    String line;    while ((line = in.readLine()) != null) {        out.write(line.trim());        out.newLine();    }}   // both are closed here, last opened first
  1. line 8 both are closed here, last opened first
Read the full explanation (about 1 min)

try-with-resources opens a resource in the parentheses after try and closes it automatically when the block ends, normally or by exception, so files, connections and streams are never leaked.

A mental model

A self-closing door: however you leave the room, it shuts behind you.

How it works

try (var in = Files.newBufferedReader(path)) { ... } calls in.close() when the block is left. Anything that implements AutoCloseable works.

Several resources are closed in reverse order of opening: the last opened is closed first.

If the body throws and close() also throws, the body's exception wins and the close failure is attached as a suppressed exception.

Going deeper

Leaked connections and file handles show up as "too many open files" or a hung connection pool long after the leaking code ran. Wrapping a resource creation in a manual try/finally is verbose and easy to get wrong; this form replaces it.

Under the hood

The compiler expands it into a try/finally that calls close() and uses addSuppressed to keep a secondary exception.

Common mistakes

  • Opening a resource outside the parentheses and forgetting to close it.
  • Ignoring suppressed exceptions when debugging.

How Unravel recognises it

When you paste code, Unravel looks for these signs of try-with-resources. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Builds on this

Frequently asked questions

What is try-with-resources?

try-with-resources opens a resource in the parentheses after try and closes it automatically when the block ends, normally or by exception, so files, connections and streams are never leaked.

What are the most common mistakes with try-with-resources?

Opening a resource outside the parentheses and forgetting to close it. Ignoring suppressed exceptions when debugging.

How can I see try-with-resources in real code?

Paste your own Java into Unravel and it will point out where try-with-resources appears, explain how it works at the detail level you choose, and draw the flow of the code around it.