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);//未处理的异常类型 Exception 13 //new exception().a(1);//可以继续抛出声明,这样子就不用try catch了 14 try { 15 new exception().a(1); 16 } catch (Exception e) { 17 System.out.println("try catch处理了异常"); 18 } 19 20 21 22 } 23 //这种写法会报错 24 /* public void a(int i) { 25 if(i==1){ 26 throw new Exception();//未处理的异常类型 Exception 27 } 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 }//未处理的异常类型 Exception 38 } 39 }*/ 40 public void a(int i) throws Exception {//声明抛出异常 41 if(i==1){ 42 throw new Exception();//抛出异常 43 } 44 } 45 46 }
自己写的demo---声明异常同时处理异常,或者继续抛出异常
---- 动动手指关注我!或许下次你又能在我这里找到你需要的答案!ZZZZW与你一起学习,一起进步!