Learn Java
import com.learnjava.exceptionhandling.MulticatchTryWithResources;

Multi-Catch & try-with-resources in Java

@Concept

Multi-catch, catch (IOException | SQLException e), lets one catch block handle several unrelated exception types identically, avoiding duplicated handling code when two different exceptions genuinely deserve the exact same response. The caught variable's type is effectively the closest common supertype of all the listed exceptions, so only methods available on all of them can be called on it.

try-with-resources, try (Resource r = new Resource()) { ... }, automatically closes any resource declared in its parentheses once the block ends, whether it exited normally or via an exception — the resource just needs to implement the AutoCloseable interface. Before it existed, safely closing a resource required a manual finally block calling close() explicitly, which was easy to get subtly wrong, since an exception thrown from within that finally block's own close() call could mask the original exception that caused the problem in the first place.

@HowItWorks
1

Multi-catch syntax

catch (IOException | SQLException e) handles either listed exception type with one shared block of code.

2

What the caught variable's type becomes

Effectively the closest common supertype of every listed exception, limiting which methods can be called on it directly.

3

try-with-resources syntax

try (Resource r = new Resource()) { ... } declares a resource that gets closed automatically once the block ends.

4

Requires AutoCloseable

Any resource used this way must implement that interface, providing its own close() method.

5

Correct even when exceptions happen

The resource is closed whether the block finishes normally or throws, and multiple resources close in reverse declaration order.

@Example
import java.io.StringReader;
 
public class ResourceDemo {
public static void main(String[] args) throws Exception {
try (StringReader reader = new StringReader("hi")) {
System.out.println("First char code: " + reader.read());
}
System.out.println("Reader closed automatically");
}
}
Output
First char code: 104
Reader closed automatically
1reader is declared inside the try's parentheses, making it a resource try-with-resources will manage automatically.
2reader.read() reads the first character's numeric code, 104, the code for the character 'h'.
3Once the try block's body finishes, reader.close() is called automatically, with no explicit finally block written.
4The final println confirms execution continues normally afterward, having relied on the automatic cleanup.
@UseCases
Handling two different exception types identically with one shared catch block
Guaranteeing a file, stream, or database connection is always closed, without a manual finally block
Avoiding an exception thrown during manual cleanup from masking the original exception that caused the problem
Managing several resources in one try statement, closed automatically in reverse order

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.