public class TryCatchFinally {
public static String output ="";
public static void foo(int i){
try{
System.out.println("try");
if(i == 1){
throw new Exception();
}
}catch(Exception e){
System.out.println("catch");
output += "2";
return ;
}finally{
System.out.println("finally");
output += "3";
}
output += "4";
}
public static void main(String[] args) {
foo(0);
foo(1);
System.out.println(output);
}
}
输出为:
try
finally
try
catch
finally
3423