Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What does each method return?

Java
static int f() {
int x = 1;
try {
return x;
} finally {
x = 2;
}
}
static int g() {
try {
return 1;
} finally {
return 2;
}
}
static StringBuilder h() {
StringBuilder sb = new StringBuilder("a");
try {
return sb;
} finally {
sb.append("b");
}
}
static int k() {
try {
throw new RuntimeException();
} finally {
return 3;
}
}

f() returns 1, because the return value is evaluated and saved before finally runs, and changing the local x afterwards does not touch the saved copy. g() returns 2, because a return in finally replaces the try's return. h() returns a StringBuilder containing ab, because the saved value is a reference and finally mutates the same object, and k() returns 3 while the exception is silently thrown away, which is why a return in finally is a bug pattern that javac -Xlint:finally warns about.

Why this question is asked

Puzzles about try and finally look like trivia, but the rules behind them explain real bugs: exceptions that vanish from logs, cleanup code that changes a result, and methods that return something different from what the code seems to say. Interviewers ask them to see whether we understand what the JVM does when a method returns from inside a try block. In this answer, we will recap the promise finally makes, then work through each of the four methods, see why a return in finally hides exceptions, and look at how the compiler can warn us.

1A Quick Recap of try and finally

A finally block runs whenever control leaves the try block: after it completes normally, after a return, after a break or continue, and after an exception. That is its whole purpose, so cleanup such as closing a file always happens.

The interesting part is the order of events when the try block contains return expr. Java first evaluates expr and saves the result in a hidden slot. Then it runs finally. Then, if finally finished normally, the method returns the saved value. The Java Language Specification calls this an abrupt completion: the try wants to complete with a return, and finally gets to run before that completion takes effect. If finally itself completes abruptly, with its own return or throw, that new completion replaces the old one and the saved value or pending exception is discarded.

Step throughWhat happens on return inside try
  1. Evaluate the return expressionthe result is saved in a hidden slot
  2. Run the finally blockit can change locals and objects, not the saved slot
  3. Did finally return or throw?
    Yesits result replaces the saved value or pending exceptionNoreturn the saved value

3 steps in order. Play them, or step through them one at a time.

The same order applies when try throws: the exception is pending while finally runs.

2F(): Changing the Local After return

Here the try block returns x, and finally sets x to 2.

Java
static int f() {
int x = 1;
try {
return x; // value 1 is saved now
} finally {
x = 2; // changes the local, not the saved value
}
}
System.out.println(f()); // 1

When return x runs, Java copies the current value of x, which is 1, into the saved slot. The finally block then changes the variable x to 2, but the saved slot is a separate copy of an int, so it still holds 1. The method returns 1. If we look at the bytecode with javap -c, we can see the compiler store x into an extra local variable before the finally code and load that extra local for the return.

3G(): A return in finally Wins

Now both blocks return.

Java
static int g() {
try {
return 1; // 1 is saved
} finally {
return 2; // finally completes abruptly with return 2
}
}
System.out.println(g()); // 2

The try saves 1 and finally runs. But finally does not finish normally; it executes its own return 2. By the rule we recapped, the new abrupt completion replaces the old one, so the saved 1 is thrown away and the method returns 2.

4H(): The Saved Value Is a Reference

This one looks like f(), but the result is different.

Java
static StringBuilder h() {
StringBuilder sb = new StringBuilder("a");
try {
return sb; // the reference to the object is saved
} finally {
sb.append("b"); // mutates the same object
}
}
System.out.println(h()); // ab

The saved slot holds a copy of the value of sb, and the value of a reference variable is the reference itself, the address of the object, not a copy of the object. finally calls append on the same object, so when the caller receives the reference, the object already contains ab. If finally had instead assigned sb = new StringBuilder("z"), the method would still return the object containing a, exactly like f(). So the rule is the same in both cases: the saved value cannot change, but the object it points to can.

5K(): The Exception That Disappears

The last method throws from try and returns from finally.

Java
static int k() {
try {
throw new RuntimeException(); // exception is pending
} finally {
return 3; // replaces the pending exception
}
}
System.out.println(k()); // 3, and no exception reaches the caller

The try block completes abruptly with an exception. While that exception is pending, finally runs and completes abruptly with return 3. Again the newer completion wins, so the exception is discarded completely. It is not logged, not wrapped, and not added as a suppressed exception; the caller simply gets 3.

This is why a return in finally is a recognised bug pattern. In real code, the try block might be a database update that failed, and the method reports success. The same happens if finally throws its own exception: the original one is lost, which is why try-with-resources was designed to attach close() failures as suppressed exceptions instead of replacing the main one. The compiler can warn us: compiling with javac -Xlint:finally reports that the finally clause cannot complete normally, and static analysis tools flag it too. The fix is simple: never return, throw or break out of finally; keep it for cleanup.

6Follow-up questions

What happens if System.exit is called inside try?

finally does not run. System.exit starts the JVM shutdown sequence and does not return to the method, so control never leaves the try block in a way that triggers finally. Registered shutdown hooks do run, but the finally block and anything after it in that thread do not. The same is true if the JVM crashes or the process is killed with SIGKILL, which is why critical cleanup cannot rely on finally alone.

If finally throws, what does the caller see?

The exception from finally. Any value the try saved, or any exception it was throwing, is discarded. That is why cleanup code that can fail should catch its own exceptions or use try-with-resources, which records close() failures as suppressed exceptions on the main one.

Does finally run when the try block has a return inside a loop with break or continue?

Yes. Any way of leaving the try block, including break and continue, runs finally first. And a break or continue inside finally discards a pending return or exception in the same way a return does.

7Conclusion

In this answer, we saw that a return in try first evaluates and saves its value, then runs finally, and only then returns the saved value. That is why f() returns 1 and h() returns the mutated StringBuilder containing ab: the saved copy cannot change, but the object a saved reference points to can. If finally itself returns, as in g() and k(), it replaces whatever was pending, including an exception, which makes a return in finally a dangerous bug pattern that javac -Xlint:finally helps us catch.

What to say in the interviewAbout 80 seconds aloud

Open with

The key rule is that when a try block executes return, Java evaluates the expression and saves the result first, then runs finally, then returns the saved value, unless finally itself returns or throws, in which case that replaces what was pending.

Then, if they want more

So f returns 1: the value 1 was saved before finally set x to 2, and changing the local does not touch the saved copy. g returns 2, because the return in finally overrides the try's return. h returns a StringBuilder containing ab.

It is the same rule as f, but the saved value is a reference, so finally appending b changes the very object the caller will receive. If finally reassigned sb instead, the caller would still get a. k returns 3, and the RuntimeException simply disappears.

It is not logged or suppressed, it is discarded, which is why return in finally is a real bug pattern: a failed operation can look like a success. javac with -Xlint:finally warns that the finally clause cannot complete normally, and static analysis flags it as well.

In practice, I keep finally for cleanup only and use try-with-resources where I can, since it keeps close failures as suppressed exceptions instead of losing the original one.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 each method return?": https://scrivio-interview-library.netlify.app/java/coding-problems/11858fd80d/