public class Test{
public static String output="";
public static void foo(int i){
try{
if(i==1){
throw new Exception();
}
output+="1";
}
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);
}
}
为什么不是134234啊,而是13423

解决方案 »

  1.   

    catch(Exception e){
    output+="2";
    return;
    }注意这里的return
    发生异常,也就是 i==1 的时候,直接return掉了,根本没有执行 +"4"这里
    如果要得到你想要的结果  134234
    就把这里的return 去掉
      

  2.   

    对,把catch里的return 去掉.return了,就不向下走了.
      

  3.   

    楼主结帖吧finally块中的语句必定被执行,所以有3,但是因为return,所以没有4,而是在执行完finally块后程序返回了
      

  4.   

    这是finally块中的语句什么时候执行的问题
    1,finally块中的语句必定被执行(执行在try,catch快之后).
    2,如果在执行try,catch快时碰到 return语句(意味着该方法执行结束)时,会先执行finally块中的语句,再执行return语句.
    结果显然就是13423