昨天在看《Java程序员面试指南》时被异常处理机制的执行顺序难到了一下,今天好好记录下。
话不多说,先上例子:
public class Test1 {
public static int testFinally(){
try {
return 1;
} catch (Exception e) {
// TODO: handle exception
return 0;
}finally{
System.out.println("execute finally");
}
}
public static void main(String[] args){
int result = testFinally();
System.out.println(result);
}
}
输出
execute finally 1
一看发现不了什么,那么稍微修改一下,看一看:
public class Test1 {
public static int testFinally(){
try {
return 1;
} catch (Exception e) {
// TODO: handle exception
return 0;
}finally{
System.out.println("execute finally");
return 3;
}
}
public static void main(String[] args){
int result = testFinally();
System.out.println(result);
}
}
输出:
execute finally 3
有些朋友估计和我一样,天了撸,竟然不是输出1 !!!
冷静思考一下,
程序执行时遇到retrun意味着结束对当前函数的调用并跳出此函数体【出栈】,
所以任何语句的执行都是在return之前(exit()函数除外)。
所以finally块里的代码也是在return之前进行的。(不信你在finally之后加return就会报错。。。)
那么话说回来,一定是finally语句块中的return覆盖了之前的return,所以才会出现输出3
接着再看下demo:
public class Test2 {
public static int testFinally(){
int result = 1;
try {
result = 2;
return result;
} catch (Exception e) {
// TODO: handle exception
return 0;
}finally{
result = 3;
System.out.println("execute finally1");
}
}
public static StringBuffer testFinally2(){
StringBuffer s = new StringBuffer("Hello");
try {
return s;
} catch (Exception e) {
// TODO: handle exception
return null;
}finally{
s.append("World");
System.out.println("execute finally2");
}
}
public static void main(String[] args){
int resultVal = testFinally();
System.out.println(resultVal);
StringBuffer resultRef = testFinally2();
System.out.println(resultRef);
}
}
输出:
execute finally1 2 execute finally2 HelloWorld
我推断try-catch-finally的执行顺序很有可能是:
顺序执行,最后执行return部分,后面的retrun覆盖之前的return语句
不过还要等到对JVM等底层机制详细了解之后,才能给出明确的解析。以后再来补吧。