try catch finally执行顺序

大家都知道
try{
}chatch(){
}finally{
}
是java中异常处理最常见的一种方法,面试中也经常会考到这方面的知识,我也看了无数遍,但总是很容易忘记,也许写出来会记忆深刻些吧.
假设try块中一个return语句的话,那么catch和finally中的代码还会执行吗?如果会执行,那么顺序又是什么?我写了个测试类分别测试了以上问题:
测试一:
class Test22
{
public static String test(){
try{
//throw new Exception();
return "return";
}catch(Exception e){
System.out.println("catch");
}finally{
return "finally";
}
}
public static void main(String[] args)
{
System.out.println(test());
}
}
输出结果为:finally
这说明finally先执行,其实我这也可以通过理解return的意义来理解这个顺序,return是指跳出这个语句块,跳出了自然不能回来再执行里面的语句了,而finally又是必须执行的,所以在跳出之前还是让finally执行完吧.

测试二:
class Test22
{
public static String test(){
try{
throw new Exception();

}catch(Exception e){
System.out.println("catch");
}finally{
return "finally";
}
}
public static void main(String[] args)
{
System.out.println(test());
}
}
输出结果:
catch
finally
这就是平常最常见的顺序了,try中势力出了异常,那么就是catch中执行了,然后就是finally里的语句了.
测试三:
class Test22
{
public static String test(){
try{
throw new Exception();
}catch(Exception e){
return "catch";
}finally{
return "finally";
}
}
public static void main(String[] args)
{
System.out.println(test());
}
}
输出结果:
finally
在catch中有return和try块中有return是一样的情况了.


这次我应该是理解得比较透彻了,希望不要忘记了.

猜你喜欢

转载自www.cnblogs.com/yongdao/p/11506129.html