ziyi
ziyi

Reputation: 31

What is missing in LocalVariableTable?

Given a simple try-catch-finally method:

public void t() {
    try {
        f1();
    }catch(Exception e) {
        f2();
    }finally {
        f3();
    }
}

With "javac -g", it's compiled into :

public void t();
  descriptor: ()V
  flags: ACC_PUBLIC
  Code:
stack=1, locals=3, args_size=1
   0: aload_0
   1: invokespecial #15                 // Method f1:()V
   4: goto          26
   7: astore_1
   8: aload_0
   9: invokespecial #18                 // Method f2:()V
  12: aload_0
  13: invokespecial #21                 // Method f3:()V
  16: goto          30
  19: astore_2
  20: aload_0
  21: invokespecial #21                 // Method f3:()V
  24: aload_2
  25: athrow
  26: aload_0
  27: invokespecial #21                 // Method f3:()V
  30: return
Exception table:
   from    to  target type
       0     4     7   Class java/lang/Exception
       0    12    19   any
LineNumberTable:
  line 7: 0
  line 8: 4
  line 9: 8
  line 11: 12
  line 10: 19
  line 11: 20
  line 12: 24
  line 11: 26
  line 13: 30
LocalVariableTable:
  Start  Length  Slot  Name   Signature
      0      31     0  this   Lsample/Sample;
      8       4     1     e   Ljava/lang/Exception;
StackMapTable: number_of_entries = 4
  frame_type = 71 /* same_locals_1_stack_item */
    stack = [ class java/lang/Exception ]
  frame_type = 75 /* same_locals_1_stack_item */
    stack = [ class java/lang/Throwable ]
  frame_type = 6 /* same */
  frame_type = 3 /* same */

Notice the locals=3, but there are only two items in the LocalVariableTable. Bytecode line 19 (astore_2) and 24 (aload_2) indicate the third local variable does exist.

The question is: what is the third local variable, and why it is missed in LocalVariableTable?

Upvotes: 1

Views: 203

Answers (1)

Antimony
Antimony

Reputation: 39451

The third local variable is an implementation detail of the finally block. In the case where your try/catch throws a variable, it has to store the exception temporarily so it can call f3() before rethrowing, and that is what slot 2 is doing.

Upvotes: 1

Related Questions