Exceptions: a return inside finally silently discards the try block's return value

When finally itself contains a return, that return wins and replaces whatever value the try block was about to return, with no warning; this is why a return inside finally is considered a bug magnet.

Code
static int compute() {
    try {
        return 1;
    } finally {
        return 2;
    }
}
System.out.println("Returned: " + compute());
Output
Returned: 2
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