try、catch、finally与return的执行顺序问题

  • finally一定会执行
  • return以最后一次为准
  • return后的finally是否修改了数据,得看具体类型

try{} catch(){}finally{} return

按照正常的顺序执行:有错会执行catchfinally都会执行,最后执行return

1
2
3
4
5
6
7
8
9
10
11
12
13
14
private static int returnInFinally_00(){
int a ;
try {
System.out.println("try...");
a = 3 / 0;
} catch (Exception e){
System.out.println("catch...");
} finally {
System.out.println("finally...");
a = 2;
}
return 100;
}
// 100

try{ return; }catch(){} finally{} return

try中已经return,所以不再执行finally后面的return

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private static int returnInFinally_01(){
int a ;
try{
System.out.println("try...");
a = 3 / 1;
return a;
} catch (Exception e){
System.out.println("catch...");
} finally {
System.out.println("finally...");
a = 2;
}
return 100;
}
// 3

try{ } catch(){return;} finally{} return

与上述情况类似

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private static int returnInFinally_02(){
int a = 0;
try{
System.out.println("try...");
a = 3 / 0;
} catch (Exception e){
System.out.println("catch...");
return a;
} finally {
System.out.println("finally...");
a = 2;
}
return a;
}
// 0

try{ return; }catch(){} finally{return;}

程序执行try块中return之前(包括return语句中的表达式运算)代码;再执行finally块,因为finally块中有return所以提前退出这提前退出的意思是指:后面的代码都不会再执行了,所以就退出)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private static int returnInFinally_03(){
int a ;
try{
System.out.println("try...");
a = 3 / 1;
return a;
} catch (Exception e){
System.out.println("catch...");
} finally {
System.out.println("finally...");
a = 2;
return a;
}
//return 100; 不可能执行此代码,编译器直接报错
}
// 2

try{} catch(){return;}finally{return;}

与上述类似

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private static int returnInFinally_04(){
int a = 0;
try{
System.out.println("try...");
a = 3 / 0;
} catch (Exception e){
System.out.println("catch...");
return a ++;
} finally {
System.out.println("finally...");
a ++;
return a;
}
}
// 2

try{ return;}catch(){return;} finally{return;}

同上述类似

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private static int returnInFinally_05(){
int a = 1;
try{
System.out.println("try...");
a = 3 / 0;
return a;
} catch (Exception e){
System.out.println("catch...");
return a ++;
} finally {
System.out.println("finally...");
a ++;
return a;
}
}
// 3

return后在finally中修改值是否会影响结果?

问题回到传值还是传引用上啦,这里有疑问就再回去看看传值与传引用吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 一个普通测试类
private static class TestObject{
private int x;

public int getX() {
return x;
}

public void setX(int x) {
this.x = x;
}
// 覆盖toString
@Override
public String toString() {
return "x = " + x;
}
}

private static TestObject returnInFinally_06(){
TestObject to = new TestObject();
try{
System.out.println("try...");
to.setX(1);// 设为1
return to;
} catch (Exception e){
System.out.println("catch...");
to.setX(2);// 设为2
return to;
} finally {
System.out.println("finally...");
to.setX(3);// 最终设为3
}
}
// try...
// finally...
// x = 3

try、catch、finally与return的执行顺序问题

https://eucham.me/2018/07/06/313d1f4f0aba.html

作者

遇寻

发布于

2018-07-06

更新于

2022-04-21

许可协议

评论