Java中为什么finally语句块一定会被执行?

10 篇文章 0 订阅

通过阅读JVM规范,得知编译器是通过冗余来实现finally语句块的。我们可以写段代码做一个验证。

JDK版本:8
如下面的代码:

import java.io.*;

public class Main {
    public static void main(String[] args) {
        try {
            foo();
        } catch (IOException e) {
            int a = 100;
        } catch (Exception e) {
            int b = 200;
        } finally {
            int c = 300;
        }
    }

    public static void foo() throws IOException {

    }
}

根据finally的语义,我们可以确定int c = 300这行代码一定会被执行。我们可以用javap -v Main来看看这段代码对应的字节码:

Code:
      stack=1, locals=5, args_size=1
         0: invokestatic  #2                  // Method foo:()V
         3: sipush        300
         6: istore_1
         7: goto          41
        10: astore_1
        11: bipush        100
        13: istore_2
        14: sipush        300
        17: istore_1
        18: goto          41
        21: astore_1
        22: sipush        200
        25: istore_2
        26: sipush        300
        29: istore_1
        30: goto          41
        33: astore_3
        34: sipush        300
        37: istore        4
        39: aload_3
        40: athrow
        41: return

其中,偏移量为14、17, 26、29, 和34、37的字节码就是int c = 300对应的字节码。sipush 300意为将300压入操作数栈,astore_N意为将操作数栈顶元素保存到本地变量表中的第N个slot中。

由此我们可以清楚地看出,编译器确实是在每个catch语句块后都添加了finally块中的字节码, try块的最后也有int c = 300字节码的冗余。如果翻译成Java代码应该这样的:

public static void main(String[] args) {
        try {
            foo();
            int c = 300; // 冗余
        } catch (IOException e) {
            int a = 100;

            int c = 300; // 冗余
        } catch (Exception e) {
            int b = 200;

            int c = 300; // 冗余
        } finally {
            int c = 300;
        }
    }

由此可知,我们在写代码时,如果finally块中的代码过多会导致字节码条数”膨胀”,因为finally中的字节码会被”复制”到try块和所有的catch块中。

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值