Learn Java
import com.learnjava.exceptionhandling.TryCatchFinally;

try, catch & finally in Java

@Concept

A try block wraps code that might throw an exception; if one is thrown, execution immediately jumps out of the try block and into the first matching catch block, skipping any remaining try-block code entirely. A catch block declares the specific exception type it handles, and only runs if a thrown exception matches that type or one of its subclasses — multiple catch blocks can follow one try, each handling a different exception type, checked in order from top to bottom.

A finally block, if present, always runs after the try/catch finishes, whether an exception was thrown or not, and even if the try or catch block contains a return statement. Its most common job is releasing a resource, like a file or a connection, that absolutely must be cleaned up regardless of how the block ends.

@HowItWorks
1

try

Wraps code that might throw an exception, allowing the rest of the program to keep running if something goes wrong inside it.

2

Immediate jump on exception

The moment an exception is thrown, execution leaves the try block right away, skipping anything remaining in it.

3

catch

Declares an exception type it handles; a thrown exception matching that type, or a subclass of it, runs that catch block's code.

4

Multiple catch blocks

Checked in order from top to bottom, with only the first matching one actually running.

5

finally

Runs after try/catch finishes no matter what, exception or not, even across a return statement, making it the standard place for guaranteed cleanup.

@Example
public class TryCatchDemo {
public static void main(String[] args) {
try {
int result = 10 / 0;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
} finally {
System.out.println("Finally block always runs");
}
}
}
Output
Caught: / by zero
Finally block always runs
110 / 0 throws an ArithmeticException immediately, so the println right after it inside the try block never runs at all.
2Execution jumps straight to the matching catch block, since its declared type matches the thrown exception.
3The catch block's println runs, reporting the exception's message.
4The finally block then runs unconditionally afterward, printing its own line regardless of what happened above it.
@UseCases
Handling a specific, anticipated failure, like dividing by zero or a missing file, without crashing the whole program
Guaranteeing a resource like a file or network connection gets closed no matter how a block exits
Catching several different exception types from the same risky block of code, each handled differently
Understanding why a finally block still runs even when the try block contains a return statement

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.