HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › try-with-resources

What javac does

try-with-resources

close() on every path, and addSuppressed when close() fails too.

try-with-resources expands into the code you would otherwise write by hand: a close() on the normal path, and a handler for any Throwable that closes the resource, attaches any exception from close() to the first one with addSuppressed, and rethrows the first.

The source

// try-with-resources: close() on every path, and suppressed exceptions.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class TryResources {
    static String firstLine(String path) throws IOException {
        try (BufferedReader in = new BufferedReader(new FileReader(path))) {
            return in.readLine();
        }
    }
}

Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.

What to look for

  • close() appears twice: after readLine() returns normally, and in the handler.
  • If close() throws while an exception is already on its way out, the inner handler calls addSuppressed, so the original exception is the one that propagates.
  • There is no null check before close() because the resource is a new expression, which cannot be null. A resource returned by a method call gets an ifnull test before each close().
  • javac 25 generates this code the same way for every --release: the flag changes the class file version and the APIs you may call, not how the statement is compiled.

TryResources.class

static String firstLine(String path)

javac --release 8 to 25 (the same bytecode for every release)

8try (BufferedReader in = new BufferedReader(new FileReader(path))) {
0new #7// BufferedReader
3dup
4new #9// FileReader
7dup
8aload_0 // path
9invokespecial #11// new FileReader(String)
12invokespecial #14// new BufferedReader(Reader)
15astore_1 // in
9return in.readLine();
16aload_1 // in
17invokevirtual #17// BufferedReader.readLine() : String
20astore_2
10}
21aload_1 // in
22invokevirtual #21// BufferedReader.close() : void
9return in.readLine();
25aload_2
26areturn
8try (BufferedReader in = new BufferedReader(new FileReader(path))) {
27astore_2 catch Throwable
28aload_1 // in
29invokevirtual #21// BufferedReader.close() : void
32goto 41
35astore_3 catch Throwable
36aload_2
37aload_3
38invokevirtual #26// Throwable.addSuppressed(Throwable) : void
41aload_2
42athrow
FromToHandlerCatches
162127java.lang.Throwable
283235java.lang.Throwable

Open in the viewer

← The enhanced for loopAll topicsfinally →

By the author of this site

Reading bytecode is half the story; running it is the other half. You execute exactly this kind of code in Build a JVM in Rust: Class files, an interpreter, objects and invokedynamic from scratch, on the real JDK class library.

Read Chapter 1 free

Java and the JVM are trademarks of Oracle; this site is not affiliated with Oracle. Field names follow the Java Virtual Machine Specification; the explanations are our own.

Sister sites: What's in my DB · LLM Anatomy · Distribution Atlas · Notation Decoder · uresk.net

Made by Spencer Uresk · About · Files you open stay on your device.