1 package exception; 2 3 public class exception { 4 public static void main(String args[]) 5 { 6 7 /*** 8 * 不能对类型 exception 中的非静态方法 a(int)进行静态引用,想要引用的时候非 9 * 静态的属性是不能够在静态的方法里引用的,想要引用的话必须是用对象来引用10 */11 //a(1);12 //new exception().a(1);//未处理的异常类型 Exception13 //new exception().a(1);//可以继续抛出声明,这样子就不用try catch了14 try {15 new exception().a(1);16 } catch (Exception e) {17 System.out.println("try\tcatch处理了异常");18 }19 20 21 22 }23 //这种写法会报错24 /* public void a(int i) {25 if(i==1){26 throw new Exception();//未处理的异常类型 Exception27 }28 }*/29 //这种写法没错,但是把异常自己处理了。相当于没卵用30 /* public void a(int i) {//抛出异常同时自己处理31 if(i==1){32 try {33 throw new Exception();34 } catch (Exception e) {35 // TODO 自动生成的 catch 块36 e.printStackTrace();37 }//未处理的异常类型 Exception38 }39 }*/40 public void a(int i) throws Exception { //声明抛出异常41 if(i==1){42 throw new Exception();//抛出异常43 }44 }45 46 }