异常返回值反思

昨天闲得无聊,忽然想起了曾经的一个疑虑:貌似有这样的线索:在catch语句里写return语句会不会真的返回,前提是有finally语句。
	int cal(){
int ret = 1;
try {ret=2;return ret;} catch(Exception e){ret=3;return ret;}
finally {
ret=4;
return ret;
}
}

这段代码,有异常的时候会返回什么结果?
反编译一下:
int cal();
Code:
Stack=1, Locals=5, Args_size=1
0: iconst_1
1: istore_1
2: iconst_2
3: istore_1
4: iload_1
5: istore_2
6: iconst_4
7: istore_1
8: iload_1
9: ireturn
10: astore_2
11: iconst_3
12: istore_1
13: iload_1
14: istore_3
15: iconst_4
16: istore_1
17: iload_1
18: ireturn
19: astore 4
21: iconst_4
22: istore_1
23: iload_1
24: ireturn
Exception table:
from to target type
2 6 10 Class java/lang/Exception

2 6 19 any
10 15 19 any
19 21 19 any
LineNumberTable:
line 3: 0
line 4: 2
line 6: 6
line 7: 8
line 4: 10
line 6: 15
line 7: 17
line 6: 19
line 7: 23


从字节码看是4。try语句块中和catch中的return都不会真的返回。只有finally中的才算数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值