一个异常学习的好例子

package 类加载器;

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);//无论是否发生异常,都会执行  1. output+3 2.output+4
       foo(1);
       System.out.println(output); //  3 .output+2  4.output+4
       //此处注意在catch中中的return ,finaly中的依然会执行
       //所以此时结果为
    }
}
原文地址:https://www.cnblogs.com/hansongjiang/p/3997156.html