Try Catch Finally Execution Order in Java
Fast recall
Quick revision
Core answer
try); if nothing throws, catch is skipped entirely. The `finally` block runs last in both cases — after try (success) or after catch (failure) — and even runs before a return. So the order is try → (catch only on error) → finally normally.Whether you can trace control flow precisely through try/catch/finally on both the success and failure paths, including the return case
Thinking the rest of the try block runs after an exception, or that finally only runs when a catch matched
State that finally runs even when there's a return in try, and even when no catch matches (the exception propagates after finally)
Interview-ready guide
Interview answer
Try starts first. If it throws, the rest of that path is skipped and Java selects the first compatible catch in source order. If nothing throws, catches are skipped. Finally then normally runs as control leaves, including when a return or uncaught exception is pending.
If finally completes normally, the pending return or exception continues. If finally returns or throws, it replaces that earlier outcome, which is why finally should contain only safe cleanup. It is also not a guarantee against process termination or code that never exits the protected region.
Try Catch Finally Execution Order in Java
Direct Answer
The try block runs first. If it throws, control jumps to the matching catch (skipping the rest of try); if nothing throws, catch is skipped entirely. The finally block runs last in both cases — after try (success) or after catch (failure) — and even runs before a return. So the order is try → (catch only on error) → finally normally.
Code Example
public class OrderFlow {
String load(boolean fail) {
try {
System.out.println("1: try start");
if (fail) throw new RuntimeException("boom");
System.out.println("2: try end"); // skipped if fail
return "ok";
} catch (RuntimeException e) {
System.out.println("3: catch"); // only when fail
return "recovered";
} finally {
System.out.println("4: finally"); // ALWAYS
}
}
}
// load(false) prints 1,2,4 -> returns "ok"
// load(true) prints 1,3,4 -> returns "recovered"
Common Mistakes & Pitfalls
- Expecting the rest of try to run after a throw: control leaves that path immediately.
- Thinking finally requires a matching catch: finally normally runs while the statement exits whether or not a catch handled the failure.
- Calling finally an unconditional guarantee: termination or code that never exits the protected region can prevent cleanup.
- Returning or throwing from finally: it can replace an earlier return or exception.
- Using finally for closeable resources: try-with-resources applies safer structured closing rules.
Check your understanding
Answer these without looking back at the article.
- What happens if both catch and finally throw an exception?
- Can you have a try block without a catch block?
- What is the purpose of the finally block?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.