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