Quick revision
The short answer
f() returns 42, and the RuntimeException("x") is lost completely: no caller ever sees it and nothing is logged. A return statement in finally ends the method normally, which discards whatever exception was propagating. javac warns about this with -Xlint:finally ("finally clause cannot complete normally"), and the rule is never to return or throw from finally.
What to remember
In this answer, we saw that f() returns 42 and the RuntimeException is lost. A finally block always runs, and if it completes abruptly with its own return, throw, break or continue, that reason replaces the pending exception or return value. javac's -Xlint:finally warning reports finally clauses that cannot complete normally, and the rule is to keep finally for cleanup only and to express fallbacks in a catch block, where the failure can be logged.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
f() returns 42, and the RuntimeException("x") is lost completely: no caller ever sees it and nothing is logged. A return statement in finally ends the method normally, which discards whatever exception was propagating. javac warns about this with -Xlint:finally ("finally clause cannot complete normally"), and the rule is never to return or throw from finally.
Why this question is asked
This looks like a trick question, but the bug it describes appears in real code, usually as a well-meant "always return a status" pattern that silently hides failures. Interviewers ask it to see whether we understand how finally interacts with the way a method completes, which is also the key to several related puzzles. In this answer, we will recap how a try statement completes, trace the method step by step, see why the exception disappears, look at the compiler warning that catches it, and finish with the rule and the correct alternative.
1A Quick Recap: How a Block Completes
The Java Language Specification describes every statement as completing either normally, meaning execution carries on to the next statement, or abruptly, for a reason. The reasons are a return, a throw, a break or a continue. A try block that throws an exception completes abruptly because of that throw.
A finally block always runs after its try block, whatever the reason the try completed. The important rule is what happens after finally. If finally completes normally, the original reason is resumed: the pending exception continues propagating, or the pending return value is returned. But if finally itself completes abruptly, with its own return or throw, its reason replaces the original one, and the original is forgotten.
2Tracing the Method
Here is the method from the question with a caller, so we can see what reaches the outside.
- try throws RuntimeException("x")an exception is now pending
- finally runsalways, before the method can exit
- Does finally complete normally?YesResume the pending exceptionNoIts own return or throw replaces it
- return 42the pending exception is discarded
- Caller receives 42no exception, no log line
5 steps in order. Play them, or step through them one at a time.
The try block throws, so a RuntimeException is pending. Before it can leave the method, finally runs. finally executes return 42, which is an abrupt completion with a reason of its own. By the rule above, that reason wins. The method returns 42 normally, and the RuntimeException is simply dropped, as if it had never been thrown.
Notice that nothing records the loss. The exception is not logged, not attached as suppressed, and not visible in any stack trace. The caller has no way to know a failure happened.
3The Same Rule With throw and break
The rule is about any abrupt completion in finally, not just return. A throw in finally replaces the pending exception with a new one, which is how old try/finally cleanup code lost root causes. A break or continue in finally inside a loop also discards the pending exception, which is even harder to spot.
So the general lesson is that finally should never change how the method exits. Its job is cleanup.
4Catching It at Compile Time
javac can warn about this. With the -Xlint:finally option, or -Xlint:all, the compiler reports any finally clause that cannot complete normally.
In a Maven or Gradle build we can enable -Xlint:all together with -Werror so that warnings fail the build. Static analysis tools such as Sonar, PMD and Error Prone also have rules against return and throw inside finally, and IDEs highlight it.
5What to Write Instead
If the intent was "return 42 when the work fails", we should say so with a catch block, where it is visible and where we can log the failure.
Now the fallback is a deliberate decision, the exception is logged with its stack trace, and finally does only what finally is for. If the cleanup itself can throw, try-with-resources is usually the better tool, because it attaches the cleanup failure as a suppressed exception instead of replacing the original.
6Follow-up questions
What happens if the try block returns 1 and finally returns 2?
The method returns 2. The try block's return is a pending abrupt completion, and the finally's return replaces it in the same way it replaced the exception. It is legal code but confusing, which is why the same warning applies.
Does finally run if the try block calls System.exit?
No. System.exit stops the JVM without completing the try statement, so finally does not run. It also does not run if the process is killed or the JVM crashes. In every normal case, return, throw, break or continue, finally runs.
Is a throw inside a catch block affected in the same way?
Yes, if there is also a finally that returns. The catch block's throw becomes the pending exception, and a return in finally discards it. This is a common way exceptions from catch blocks seem to vanish in old code.
7Conclusion
In this answer, we saw that f() returns 42 and the RuntimeException is lost. A finally block always runs, and if it completes abruptly with its own return, throw, break or continue, that reason replaces the pending exception or return value. javac's -Xlint:finally warning reports finally clauses that cannot complete normally, and the rule is to keep finally for cleanup only and to express fallbacks in a catch block, where the failure can be logged.
Open with
It returns 42, and the exception is gone. The try block throws, so a RuntimeException is pending, then finally runs before the method can exit. The language rule is that if finally completes normally, the pending exception continues, but if finally completes abruptly, with its own return or throw, that replaces the pending one.
Then, if they want more
Here finally returns 42, so the method returns normally and the exception is discarded completely. It is not logged or attached as suppressed, so the caller has no idea anything failed.
The same happens with a throw in finally, which replaces the original exception, or a break in finally inside a loop. javac catches this with -Xlint:finally, which prints finally clause cannot complete normally, and I would enable lint warnings as errors in the build, plus the matching Sonar or PMD rule.
My rule is never to return or throw from finally and to use it only for cleanup. If the intention was to return a default value on failure, I would write that in a catch block, log the exception there, and return the default explicitly.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does this method return, and what happens to the exception?": https://scrivio-interview-library.netlify.app/java/exceptions/2240b7481b/