测试代码:
public static void main(String[] args){
MainTest t = new MainTest();
System.out.println(t.test(2));
}
public String test(int i){
try{
if(i==1){
this.e();
}
return "try";
}catch(Exception e){
@SuppressWarnings("unused")
String s = "aaa";
return "catch";
}finally{
return "finally";
}
}
public void e() throws Exception{
throw new Exception();
}
当无异常捕获时,输出finally
当捕获到异常时,输出finally
总结:当发现return时,对,就在return要执行之前,会执行finally里的代码,所以是先执行finally再执行return。