1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {
callMethod();
}
private static int callMethod() {
try {
System.out.println("call method");
return i();
}catch (Exception ex){

}finally {
System.out.println("finally block ");
}
return -1;
}
private static int i(){
System.out.println("i method");
return 1;
}

返回值

1
2
3
call method
i method
finally block

最近看了一篇微信文章,感觉有些错误,
地址: https://mp.weixin.qq.com/s/-O63GbePkT8jZdeGBmBXWQ
第二十六题
try{}里有一个return语句,那么紧跟在这个try后的finally{}里的code会不会被执行,什么时候被执行,在return前还是后?
我自己做了个测试,如下,大家可以体会下.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void main(String[] args) {
callMethod();
}
private static int callMethod() {
try {
System.out.println("call method");
return i();
}catch (Exception ex){

}finally {
System.out.println("finally block ");
}
return -1;
}
private static int i(){
System.out.println("i method");
return 1;
}

返回值

1
2
3
call method
i method
finally block