Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

How does javac compile finally blocks, and why can large finally blocks matter for method size and JIT compilation?

javac compiles a finally block by copying its code onto every exit path of the try: once for normal completion, once before each return, break or continue, and once in a catch-all exception handler that runs the code and rethrows. Very old compilers shared one copy through jsr/ret instructions, which class files of version 51 (Java 7) and later may not contain. Because of the copies, a large finally block multiplies bytecode size, which can push a method past the JIT's inlining limits or even its huge-method limit, so finally blocks should stay short and delegate to methods.

Why this question is asked

Most of the time we never think about what javac produces, but finally blocks are a place where the source and the bytecode look very different, and the difference shows up in performance work and in odd compile errors. Interviewers ask this to see whether we can reason about the JVM below the language. In this answer, we will recap what a finally block must guarantee, look at the bytecode javac generates, see the history of jsr/ret, understand why size matters for the JIT, and look at what try-with-resources adds.

1What a finally Block Must Guarantee

A finally block must run however the try block ends: by falling off the end, by a return, by a break or continue that leaves the try, or by an exception, including one we did not catch. In bytecode, these are all different paths. A return is an ireturn or areturn instruction, a break is a goto to some later address, and an exception is a jump to a handler listed in the method's exception table, a table of address ranges and the handler address for each.

Bytecode has no instruction meaning "run this cleanup on every exit". So the compiler has to make sure the cleanup code is reached on every one of those paths itself.

2Looking at the Bytecode

Take a method with two return paths and a finally block, and look at it with javap -c, the JDK's disassembler.

Java
static int f(boolean b) {
try {
if (b) return 1;
work();
return 2;
} finally {
cleanup();
}
}
// javap -c, abbreviated; constant pool numbers vary
0: iload_0
1: ifeq 11
4: iconst_1
5: istore_1
6: invokestatic #2 // cleanup() copy 1: before return 1
9: iload_1
10: ireturn
11: invokestatic #3 // work()
14: iconst_2
15: istore_1
16: invokestatic #2 // cleanup() copy 2: before return 2
19: iload_1
20: ireturn
21: astore_2 // catch-all handler: save the exception
22: invokestatic #2 // cleanup() copy 3: exception path
25: aload_2
26: athrow // rethrow the original exception
Exception table:
from to target type
0 6 21 any
11 16 21 any
Step throughOne finally block, three copies
  1. try bodycovered by a catch-all (any) handler
  2. Exit by return 1?
    YesCopy 1 of finally, then ireturnNoContinue
  3. Exit by return 2?
    YesCopy 2 of finally, then ireturnNoException thrown
  4. Catch-all handlerstore the exception, copy 3 of finally, athrow

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

Every exit path gets its own copy of the finally code.

Notice that cleanup() appears three times. Before each return, javac stores the return value in a local slot, inserts the finally code, then loads the value and returns. For exceptions, it adds an entry of type any to the exception table, which catches every Throwable thrown in the try range; the handler stores the exception, runs the finally code and rethrows it with athrow.

Also notice the ranges in the exception table stop before each inlined copy. The reason is that an exception thrown by cleanup() itself must not jump into the handler and run cleanup() a second time.

3The History of jsr and ret

The original JVM design had a way to avoid the copies. The jsr instruction jumped to a subroutine and pushed a return address, and ret jumped back to it. Old javac compiled the finally block once as a subroutine and called it with jsr from every exit path, so the code existed only once.

The problem was verification. The JVM verifies bytecode before running it, and subroutines made the analysis of types in local variables difficult and slow, a long-standing source of verifier complexity and bugs. When Java 6 introduced the faster type-checking verifier based on StackMapTable attributes, jsr and ret did not fit, and javac had already moved to inlining. The JVM specification now says that class files of version 51.0 (Java 7) or above must not contain jsr or ret. Modern compilers always duplicate.

4Why Size Matters for the JIT

Duplication is invisible at runtime for small finally blocks, but bytecode size has real consequences. HotSpot's JIT compilers decide whether to inline a method, copy its body into the caller to avoid the call and enable further optimizations, partly by bytecode size. By default on common platforms, a method is inlined if it is at most 35 bytes (MaxInlineSize), or up to 325 bytes if it is hot (FreqInlineSize); check your JVM version, since these defaults can differ by platform.

There is also a much larger limit. By default, HotSpot does not JIT-compile a method whose bytecode is larger than 8,000 bytes (the HugeMethodLimit, with DontCompileHugeMethods enabled). Such a method runs in the interpreter forever, which can be ten or more times slower. And javac itself refuses methods whose bytecode exceeds 65,535 bytes with the error code too large.

So suppose a finally block contains 200 bytes of logging and metrics code, and the try has five return statements. That is six copies, about 1,200 bytes, inside one method. A method that was a candidate for inlining no longer is, and generated code, such as large switch statements from code generators, can cross the 8,000-byte limit and quietly stay interpreted.

Java
// Prints JIT decisions; look for "too big" or "hot method too big" next to your method
java -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlining -jar app.jar
// A huge method can be confirmed with -XX:+PrintCompilation: it never appears in the output

5Keeping finally Blocks Small

The fix is simple: keep the finally block to a call or two, and move the logic into a separate method. The copies are then a few bytes each, and the separate method can itself be compiled and inlined on its own merits.

Java
try {
return process(request);
} finally {
recordMetrics(request, start); // one small call, copied to each exit path
}

Reducing the number of exit paths also helps: a method with one return has fewer copies than one with five.

6What try-with-resources Adds

try-with-resources is compiled in a similar way, with extra code for suppressed exceptions. On the normal path, javac inserts a call to close(). On the exception path, it catches the Throwable, calls close() inside its own try, and if close() throws, calls addSuppressed on the original before rethrowing it. With several resources, this structure nests once per resource.

Older javac versions generated noticeably larger code for this, including null checks on the resource and a helper structure; recent versions produce a more compact form (check your version). Either way, the generated code is larger than a plain finally, which is one more reason to keep the bodies of such methods focused.

7Follow-up questions

Does the duplication affect the behaviour of the program?

No. Each copy is the same code, and exactly one copy runs on any given exit, so the program behaves exactly as the source says. The cost is only in bytecode size and what that does to JIT decisions.

Can we see that a method was not compiled because it is huge?

Yes. With -XX:+PrintCompilation the method never appears in the compiled list, and a profiler such as JDK Flight Recorder or async-profiler shows time spent in interpreted frames for it. Turning off DontCompileHugeMethods is possible but is a workaround; splitting the method is the real fix.

Why does the exception table range skip the inlined copies?

Because an exception thrown from the finally code itself must propagate normally. If the range covered the copies, an exception in cleanup() would jump to the catch-all handler, run cleanup() again and possibly loop.

8Conclusion

In this answer, we saw that javac duplicates the finally code on every exit path: before each return or jump, and in a catch-all exception handler that rethrows. The old jsr/ret subroutine approach was dropped and is forbidden in class files of version 51 and later. The duplication increases bytecode size, which can stop a method from being inlined or, past 8,000 bytes by default, from being compiled at all. So we keep finally blocks short, move logic into methods, and remember that try-with-resources generates similar code plus suppressed-exception handling.

What to say in the interviewAbout 90 seconds aloud

Open with

javac inlines the finally block on every exit path. If the try block has two returns, the finally code is copied before each return, after the return value has been stored in a local slot.

Then, if they want more

Then there is one more copy in a catch-all handler, registered in the exception table with type any, which stores the exception, runs the finally code and rethrows it. The exception table ranges deliberately exclude the copies, so an exception from the finally code does not loop back into the handler.

Very old compilers used jsr and ret to call the finally block as a subroutine, but that complicated verification, and class files from version 51, Java 7, onwards are not allowed to contain jsr or ret. The cost of duplication is bytecode size.

HotSpot inlines small methods based on size, around 35 bytes normally and 325 for hot methods by default, and does not compile methods over 8,000 bytes at all, so a big finally block in a method with many exits can stop inlining or leave the method interpreted. javac also has a hard 64 KB per method limit.

So I keep finally blocks to a call or two and move the logic into its own method. try-with-resources compiles similarly, with extra code that calls close and adds suppressed exceptions.

About this answer

Last updated
12 November 2026
Versions it names
Java 7, Java 6
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.
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 "How does javac compile finally blocks, and why can large finally blocks matter for...": https://scrivio-interview-library.netlify.app/java/exceptions/abb8e8aa89/