Intermediate · Exceptions and generics · topic 28 of 60 · 20 min
try-with-resources and clean-up
Explanation
Why closing matters
Operating systems and databases give each program a limited number of open files and connections. Forgetting to close them causes a resource leak: after a few hours a server starts failing with "Too many open files" or "connection pool exhausted". Closing must happen even when an error occurs halfway.
The old way: finally
java.io.BufferedReader reader = null;
try {
reader = new java.io.BufferedReader(new java.io.FileReader("orders.csv"));
System.out.println(reader.readLine());
} catch (java.io.IOException e) {
System.out.println("Read failed: " + e.getMessage());
} finally {
if (reader != null) {
try { reader.close(); } catch (java.io.IOException ignored) { }
}
}The modern way: try-with-resources (Java 7+)
import java.io.*;
try (BufferedReader reader = new BufferedReader(new FileReader("orders.csv"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println("Read failed: " + e.getMessage());
}
// reader.close() has been called automatically hereSeveral resources can be opened in one try, separated by semicolons; they are closed in the REVERSE order of opening. The resource variable is final inside the block.
try (var in = new FileInputStream("photo.jpg");
var out = new FileOutputStream("copy.jpg")) {
in.transferTo(out); // Java 9+
} // out is closed first, then inAutoCloseable: make your own resource
class Timer implements AutoCloseable {
private final long start = System.nanoTime();
private final String label;
Timer(String label) { this.label = label; }
@Override public void close() {
System.out.printf("%s took %d ms%n", label, (System.nanoTime() - start) / 1_000_000);
}
}
try (Timer t = new Timer("GST report")) {
buildReport();
} // prints "GST report took … ms" automaticallySuppressed exceptions
If the try block throws and then close() also throws, try-with-resources keeps the FIRST exception as the main one and attaches the close() exception to it as a suppressed exception (e.getSuppressed()). The old finally style would lose the first exception.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
try-with-resources and clean-up · 1/6
In this lesson you will close files and connections safely with try-with-resources.
Prefer a video by a teacher? Find free videos on “java try with resources autocloseable explained” on YouTube ↗
In real life: A nightly report that crashed the server
A payroll server crashed every few days with "Too many open files". The report code opened a FileWriter for each employee and closed it only on success; whenever a record had bad data, the exception skipped the close. Switching to try-with-resources fixed the leak.
for (Employee e : employees) {
try (var w = new java.io.FileWriter("slips/" + e.id() + ".txt")) {
w.write(slipFor(e));
} catch (java.io.IOException ex) {
System.err.println("Slip failed for " + e.id() + ": " + ex.getMessage());
}
}Practice (0/3 done)
try (A a = new A(); B b = new B()) { } — in which order are they closed?
Which interface must a class implement to be used in try-with-resources?
Write a method static int countLines(String path) that counts the lines of a text file using BufferedReader in try-with-resources, and returns -1 (after printing a message) if an IOException happens. Test it with an existing file and a missing one.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- GenericsExceptions and generics · same module
- Exceptions: try, catch, finally and custom exceptionsC# and .NET · Exceptions, delegates, events and async
- Context managers and the with statementPython Programming · Advanced Python
- Error handling: try, catch, finally and throwJavaScript Programming (language in depth) · Modules, errors, JSON and dates
- Errors and exceptionsPython Programming · Errors, modules and the standard library
- Exceptions: throw, try, catch and noexceptC++ Programming (modern C++ and DSA) · Exceptions, concurrency and building projects with CMake
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What problem does try-with-resources solve?
Answer: Forgetting to close resources, especially when exceptions happen
It closes automatically.
Since which Java version is try-with-resources available?
Answer: Java 7
Introduced in Java 7.
What is a suppressed exception?
Answer: An exception thrown by close() while another exception was already in flight
It is attached to the main exception.
var c = open(); try { use(c); } catch (Exception e) {} — is c closed automatically?
Answer: No, only resources declared in try ( … ) are closed
It must be declared in the resource specification (or listed there).
What happens to an exception nobody catches in main?Related
Answer: The thread ends and the stack trace is printed
Uncaught exceptions end the thread.
A custom exception class should inherit from...Related
Answer: Exception
Custom exceptions derive from Exception (or a more specific exception).
When does the clean-up part of a context manager run?Related
Answer: Always, when the with block ends for any reason
That is the whole point: clean-up runs whether the block finished normally or raised an error.
When does finally run?Related
Answer: Always, after try (and catch)
finally runs in every case, even after return.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Exceptions and generics” before its exam: Generics, Avoiding NullPointerException.
- Module 6 exam: Exceptions and generics · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, The collections framework. 60 topics left in the course before the final exam.
← Previous: Checked vs unchecked exceptions and throwing your own