A return statement in finally does not just override a returned value, it also swallows any exception that was in flight from the try block, so the caller never learns the operation failed at all.
static int risky() {
try {
throw new IllegalStateException("boom");
} finally {
return -1;
}
}
System.out.println("Returned despite the throw: " + risky());
Returned despite the throw: -1
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27