Try-With-Resources Without Catch Block
Fast recall
Quick revision
Core answer
try (Resource r = ...) { ... } is valid. The resource is still auto-closed, and any exception simply propagates to the caller. You add a catch only if you actually want to handle the exception here; otherwise the bare form is perfectly fine.Whether you know try-with-resources is itself a complete statement that doesn't require catch or finally
Thinking you must add a catch (or finally) when using try-with-resources
Clarify that the implicit close acts like a finally, so adding an explicit finally is usually redundant
Interview-ready guide
Interview answer
Yes, you can use try-with-resources completely on its own, with no catch block and no finally block.
Normally the rule is that a plain try has to be followed by a catch or a finally, but with try-with-resources, the resource declaration itself satisfies that requirement, because it carries an implicit close that behaves just like a finally. So writing try, open your resource in the parentheses, use it in the body, and then nothing after the closing brace — that compiles and runs perfectly.
When the block exits, the resource's close method is called automatically, and if an exception was thrown, it just propagates up to the caller unhandled. The one thing you need to remember is that if the exception is checked, like an I O exception, and you're not catching it, then your method has to declare it with throws, because you're passing the responsibility up the chain.
You only add a catch block when you genuinely want to handle the error at this level — for instance, to log it and return a default value. And adding an explicit finally is allowed, but for the purpose of closing the resource it's redundant, because that's exactly what try-with-resources is already doing for you.
The only reason you'd add a finally is if you have some additional cleanup beyond closing the declared resources. A mistake I'd avoid is adding an empty catch block just to dodge declaring throws, because that swallows the exception and hides problems. If you can't handle it meaningfully, it's better to let it propagate.
Try-With-Resources Without Catch Block
Direct Answer
Yes. try-with-resources can stand alone with no catch and no finally — try (Resource r = ...) { ... } is valid. The resource is still auto-closed, and any exception simply propagates to the caller. You add a catch only if you actually want to handle the exception here; otherwise the bare form is perfectly fine.
Code Example
The first method has no catch and propagates IOException; add a catch only when you can recover.
// Valid: no catch, no finally -- the resource is still auto-closed.
public List<String> readLines(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.lines().toList();
}
// close() runs automatically; IOException propagates to the caller.
}
// Add catch ONLY if you want to handle it here:
public List<String> readLinesSafe(String path) {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.lines().toList();
} catch (IOException e) {
return List.of(); // graceful fallback
}
}
Common Mistakes & Pitfalls
- Thinking a
catchis mandatory. The implicit close satisfies thetryrequirement. - Adding a redundant
finallyto close — try-with-resources already closes for you. - Forgetting to declare/propagate the checked exception when there's no
catch(throws IOException). - Swallowing with an empty
catchjust to avoid declaringthrows— let it propagate instead.
Check your understanding
Answer these without looking back at the article.
- Can you add a finally block to try-with-resources?
- What is the order of operations when try-with-resources has both a catch and a finally?
- Is try-with-resources just syntactic sugar for try-finally?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.