昨天闲得无聊,忽然想起了曾经的一个疑虑:貌似有这样的线索:在catch语句里写return语句会不会真的返回,前提是有finally语句。
这段代码,有异常的时候会返回什么结果?
反编译一下:
从字节码看是4。try语句块中和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中的才算数。