Exceptions: a return inside finally silently discards a propagating exception too

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.

Code
static int risky() {
    try {
        throw new IllegalStateException("boom");
    } finally {
        return -1;
    }
}
System.out.println("Returned despite the throw: " + risky());
Output
Returned despite the throw: -1
Advertisement
More in JAVA

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

© Java Coding Hub · About · Contact · Privacy · Terms