关于try、catch、finally的执行顺序

先来看牛客上的一道题
public class TestDemo {
public static String output =””;
public static void foo(int i){
try{
if(i == 1){
throw new Exception();
}
}catch(Exception e){
output += “2”;
return ;
}finally{
output += “3”;
}
output += “4”;
}

public static void main(String[] args) {
foo(0);
foo(1);
System.out.println(output);
}
}
当执行foo(0)时,首先进入try块,不满足,继而进入finally,最后执行try-catch-finally外部的代码,所以output变为“34”, 然后是foo(1),进入try块,try中抛出异常,有匹配的catch语句,则catch语句捕获,然后,因为catch中有return语句,则return要在finally执行后再执行;try-catch-finally之外的代码就不再执行了(因为有return打断),所以最终output的值为“3423”如果这个例子中的catch语句没有return,那么输出的结果就应该是“34234”. 从此例可以看出亮点:1、try中没有抛出异常,则catch语句不执行,如果有finally语句,则接着执行finally语句,继而接着执行finally之后的语句;2.不管是否try…catch,finally都会被执行。当try…catch中有return的话,finally后会执行try…catch中的return,然后不再执行后续语句。也就是说finally字句中的语句总会执行,即使有return语句,也是在return之前执行。3、还有一点:finally前有return、finally块中也有return,先执行前面的return,保存下来,再执行finally的return,覆盖之前的结果,并返回。
再一个例子
public class Test
{
public static int aMethod(int i)throws Exception
{
try{
return i / 10;
}
catch (Exception ex)
{
throw new Exception(“exception in a Method”);
} finally{
System.out.printf(“finally”);
}
}

public static void main(String [] args)
{
try
{
aMethod(0);
}
catch (Exception ex)
{
System.out.printf(“exception in main”);
}
System.out.printf(“finished”);
}
}
此题输出结果为 finally finished如果将aMethod中的i/10换成10/i,则输出结果为 finally exception in main finished

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理