今天遇到一个有趣问题,就是关于 try 、catch 、finally 和 return的顺序问题,本来觉得自己理解的不错,但是结果还是大跌眼镜,特意记录一下
本文参考了 https://www.jianshu.com/p/06755f52ba90,感谢原作者
try里有return,finally怎么执行
- finally块里的代码是在return之前执行的
public class Test {
public static int testFinally() {
try {
return 1;
} catch (Exception ex) {
return 2;
} finally {
System.out.println("execute finally");
}
}
public static void main(String[] args) {
int result = testFinally();
System.out.println(result);
}
}
执行结果是:
execute finally
1
- 但是如果catch里也有return ,那么catch的return会执行,而try里的return语句得不到执行的机会
public class Test {
public static int testFinally() {
try {
int i =1/0;
return 1;
} catch (Exception ex) {
return 2;
} finally {
System.out.println("execute finally");
}
}
public static void main(String[] args) {
int result = testFinally();
System.out.println(result);
}
}
执行结果:
execute finally
2
- 此外,如果try-catch-finally中都有return,那么finally块中的return将会覆盖别处的return语句,最终返回到调用者那里的是finally中return的值。
public class Test {
public static int testFinally() {
try {
return 1;
} catch (Exception ex) {
return 2;
} finally {
System.out.println("execute finally");
return 3;
}
}
public static void main(String[] args) {
int result = testFinally();
System.out.println(result);
}
}
执行结果:
execute finally
3
finally里return 改变返回值,对返回值的影响
- 在try/catch中有return时,在finally块中改变基本类型的数据对返回值没有任何影响;而在finally中改变引用类型的数据会对返回结果有影响。
/**
* try/catch中有return,在finally{}中改变基本数据类型、引用类型对运行结果的影响
*/
public class Test {
public static int testFinally1() {
int result1 = 1;
try {
return result1;
} catch (Exception ex) {
result1 = 2;
return result1;
} finally {
result1 = 3;
System.out.println("execute testFinally1");
}
}
public static StringBuffer testFinally2() {
StringBuffer result2 = new StringBuffer("hello");
try {
return result2;
} catch (Exception ex) {
return null;
} finally {
result2.append("world");
System.out.println("execute testFinally2");
}
}
public static void main(String[] args) {
int test1 = testFinally1();
System.out.println(test1);
StringBuffer test2 = testFinally2();
System.out.println(test2);
}
}
execute testFinally1
1
execute testFinally2
helloworld
原文中作者,也提到这个问题,但是我觉得解释的并不是令人信服,后来通过网上找资料,查找资料得到的答案是利用字节码(javap)命令,这里需要看字节码(无奈不太会分析字节码,具体是javap -v -p xxx.class,在代码区能看到iload压栈操作)
其实在return操作前,会将当前值压栈,然后又由于java是值传递的,导致改变传统类型的值不会影响return的值(return的操作就是出栈),
而对象类型我的猜测是由于压入的是地址,所以改变对象,会影响return的值
但是对于String这个对象就不一样了,String对象是不可变的,每次修改其实都是新建该类型对象
public class returnTest {
public int test() {
int a = 3;
try {
System.out.println("a的值="+a);
return a;
} finally {
a = 10;
System.out.println("finally a的值="+a);
}
}
public static void main(String[] args) {
returnTest rt = new returnTest();
System.out.println("返回的值"+testFinally2());
}
public static String testFinally2() {
String result2 = new String("hello");
try {
return result2;
} catch (Exception ex) {
return null;
} finally {
result2+="world";
System.out.println("execute testFinally2");
}
}
}
运行结果:
execute testFinally2
返回的值hello
实际返回就是原来压人栈的对象
finally块一定会被执行到吗?
不一定,需要两个前提条件:对应 try 语句块被执行 && 程序正常运行
正常运行即 JVM 没有退出或者线程没有被 killed(例如system.exit(0))、interrupted