Return in Finally Block Java Gotcha
Fast recall
Quick revision
Core answer
return inside a finally block overrides any return value or exception from the try/catch — and it does so silently. If try was returning a value, that value is replaced; if try was propagating an exception, the exception is swallowed entirely. This is a well-known anti-pattern: never put return (or throw) in finally.Whether you understand that finally can override the try's return value and even suppress a pending exception
Not realising a return in finally discards the try block's return value or in-flight exception
Explain that this is why static analysis tools flag 'return in finally', and that the same applies to throw in finally
Interview-ready guide
Interview answer
A return statement inside a finally block is a real gotcha, because finally normally runs when control leaves the protected code before control leaves the method, which means a return in finally effectively gets the last word.
So if your try block had already computed a return value, the return in finally overrides it and that original value is just discarded. But the more dangerous case is when the try block was in the middle of throwing an exception. If finally then does a return, the method returns normally and the exception is silently swallowed — the caller never even finds out that anything went wrong.
That's a debugging nightmare, because the error completely disappears without a trace. The exact same thing happens if you throw from finally instead of returning — the finally's exception replaces whatever the try was throwing, so you lose the original cause. This is why putting a return or a throw inside finally is universally considered an anti-pattern, and why static analysis tools and linters specifically flag a return in finally.
The mental model I'd give is that finally is purely for cleanup — closing a stream, releasing a lock, resetting some state. It should never be the thing that decides what the method returns or what exception it throws. Let the try and catch blocks own the outcome, and let finally just quietly clean up on the way out.
So if I saw a return in a finally block during a code review, I'd flag it immediately, because at best it's confusing and at worst it's hiding exceptions that the rest of the system needs to see.
Return in Finally Block Java Gotcha
Direct Answer
A return inside a finally block overrides any return value or exception from the try/catch — and it does so silently. If try was returning a value, that value is replaced; if try was propagating an exception, the exception is swallowed entirely. This is a well-known anti-pattern: never put return (or throw) in finally.
Common Mistakes & Pitfalls
returninfinally— overrides thetry's value and swallows pending exceptions.throwinfinally— replaces the original exception, losing the real cause.- Mutating the return value in
finally— for primitives the already-computed value is returned, which confuses readers. - Ignoring linter warnings — tools flag 'finally returns' precisely because of this trap.
Check your understanding
Answer these without looking back at the article.
- What does SonarQube flag about return in finally?
- Can finally swallow an exception without a return statement?
- How do you detect silently swallowed exceptions in code review?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.