Return in Finally Block: Override and Suppression
In a try-finally block, what happens to a return statement inside the finally block?
Quick Answer
The finally block's return statement wins because Java guarantees that a finally block always executes once a try block is entered, and if that finally block itself contains a return statement, its value replaces whatever the try block was about to return, so the try block's return is effectively abandoned mid-flight rather than ignored from the start. This holds true even in more dramatic scenarios: if the try block throws an exception, a return statement in finally still executes and suppresses that exception entirely, because finally is the last word on how the method exits, no matter what happened before it. The alternative explanations fail for related but distinct reasons: it is not that finally only sometimes runs, since it always runs regardless of a return earlier in the block, and it is not that the earlier return somehow takes precedence, since finally is structurally guaranteed to be the final code that executes before control leaves the method. This behavior is exactly why experienced Java developers are cautioned against putting return statements inside finally blocks in real code, even though it is legal, since it can silently discard results or swallow exceptions. On the exam, whenever you see a return, or a thrown exception, in both try and finally, the finally block's outcome is always the one that actually determines what the method produces.
⚠ Common exam trap
Candidates often assume the finally block only runs for cleanup and cannot affect the return value, but the Java Language Specification explicitly allows a return in finally to override any prior return. Be careful with exception overriding: while it's true that a return in finally can suppress an exception, this question focuses strictly on the return statement's effect on return values.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The return in the finally block is executed, and the try block return is ignored.
In a try-finally block, the finally block is always executed. If the finally block contains a return statement, that return value overrides any return value from the try block. Therefore, option A is correct. Option B is incorrect because the finally block always executes regardless of a return in try. Option C is incorrect because the finally return overrides the try return. Option D is incorrect because a return in finally is executed even if an exception occurs, overriding the exception.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
The return in the finally block is executed, and the try block return is ignored.
Why this is correct
Correct. The finally block's return is executed and its value is returned, ignoring any prior return in try.
- ✗
The finally block is skipped if there is a return in try.
Why it's wrong here
Incorrect. The finally block always executes even if there is a return in try.
- ✗
The return in the try block takes precedence.
Why it's wrong here
Incorrect. The return in finally takes precedence over the return in try.
- ✗
If the try block throws an exception, the finally block's return is skipped.
Why it's wrong here
Incorrect. While it is true that a finally return can suppress an exception, the question does not ask about exceptions; it asks about the return statement itself. The primary behavior is that it overrides any return in try, not that it overrides an exception. Additionally, if there is no exception, D is not applicable.
Go deeper
Related to this question
About these practice questions
One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on 1Z0-829
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. What happens when a finally block throws an exception?
easy- A.The JVM logs both exceptions and then terminates the thread.
- B.The exception from the try block is propagated and the finally exception is ignored.
- C.Both exceptions are reported as suppressed exceptions of a new exception.
- ✓ D.The exception from the try block is suppressed and the finally exception is reported.
Why D: When a finally block throws an exception, the JVM suppresses the exception that was thrown from the try block (if any) and propagates the exception thrown from the finally block. This behavior is defined in the Java Language Specification (JLS §14.20.2) and ensures that the finally block's exception takes precedence, as the finally block is executed after the try block completes abruptly.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.