当前位置 博文首页 > 近未来:try-catch-finally的执行顺序
结论:
分有return和没有return来讨论try-catch-finally执行顺序的情况:
举个例子:
try {
t();
tt();
} catch (Exception e) {
c();
} finally {
f();
}
m();
因为没有捕捉到异常,那么执行try而不会执行catch,而finally无论如何都要执行。
其执行顺序为:t() --> tt() --> f() --> m()
当 t() 中抛出异常,那么 t() 中从抛出异常的语句之后的代码以及 tt() 都不会执行, 程序会尝试捕捉异常。
捕捉Exception,捕捉成功,执行 c() ;
一旦捕捉到一个异常,不会再尝试捕捉其他异常,直接执行finally里的f();
执行后面的函数 m() 。
其执行顺序为:t() --> c() --> f() --> m()
try{}
catch(){}
finally{}
return;
程序按顺序执行
try{return 1;}
catch(){}
finally{}
return 2;
程序执行try块中return之前(包括return语句中的表达式运算)代码;
再执行finally块,最后执行try中return;
finally块之后的语句return不再执行,因为程序在try中已经return过了。
结论:得到try中的返回值1。
try{}
catch(){return 1;}
finally{}
return 2;
程序先执行try,如果遇到异常执行catch块,
try{return 1;}
catch(){}
finally{return 2;}
try{}
catch(){return 1;}
finally{return 2;}
try{return 1;}
catch(){return 2;}
finally{return 3;}
程序执行try块中return之前(包括return语句中的表达式运算)代码;
无return,无异常
try ->finally
无return,有异常
try(未出现异常的前半段) -> catch ->finally
try或catch中有return,无异常
try -> finally ->return(try)
try或catch中有return,有异常
try(未出现异常的前半段) -> catch ->finally->return(catch)
只要是finally中有return的情况
不论有没有异常,try或catch中有没有return
try/catch->return(finally)
我们可以看出当finally中有return的时候,相当于此代码肯定会返回该值。
public class ExceptionTest {
int age;
public static void main(String[] args) {
System.out.println(test().age);
}
public static ExceptionTest test() {
ExceptionTest test = new ExceptionTest();
try {
test.age = 10;
return test;
}catch (Exception e) {
test.age = 20;
return test;
}finally {
test = new ExceptionTest();
test.age = 30;
}
}
}
输出:10
当 try-catch 块中有return 语句时,return 语句会暂存变量值,然后执行 finally 代码块。