当出现程序无法控制的外部环境问题(用户提供的文件不存在,文件内容损坏,环境,网络不可用...)时,Java就会用异常对象来描述。Java中用2种方法处理异常:

  Ⅰ.在发生异常的地方直接处理;

  Ⅱ.将异常抛给调用者,让调用者处理

 

异常分类

  不正常的情况就分成了两大类,这个两个的老大是Throwable,一般不可处理的是Error, 可以处理的是Exception

  Ⅰ.编译时被检测异常:只要是Exception和其子类都是,除了特殊子类RuntimeException体系。这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。
这样的问题都可以针对性的处理。
  Ⅱ.编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类。这种问题的发生,无法让功能继续,运行无法进行,更多是因为调用者的原因导致的而或者引发了内部状态的改变导致的。那么这种问题一般不处理,直接编译通过,在运行时,让调用者调用时的程序强制停止。让调用者对代码进行修正。

 

异常抛出与声明

 

 1 //用throw给调用者抛出异常
 2 public class Demo {
 3     public int method(int[] arr, int index)
 4     {
 5         if(arr == null)
 6         {
 7             throw new NullPointerException("数组不能为空");
 8         }
 9         if(index >= arr.length)
10         {
11             throw new ArrayIndexOutOfBoundsException("数组角标越界了");
12         }
13         if(index < 0)
14         {
15             throw new ArrayIndexOutOfBoundsException("下标不能为负数");
16         }
17         return arr[index];
18     }
19 }
20 
21 public class ExceptionDemo {
22     public static void main(String[] args)
23     {
24         int[] arr = new int[3];
25         Demo demo = new Demo();
26         demo.method(arr,3);
27     }
28 }

自定义异常,要么继承Exception,要么继承EuntimeException

 1 //自定义异常类
 2 class FushuIndexException extends Exception
 3 {
 4     FushuIndexException(){}
 5     FushuIndexException(String msg){
 6         super(msg);
 7     }
 8 }
 9 
10 public class Demo {
11     //异常声明
12     public int method(int[] arr, int index) throws FushuIndexException
13     {
14         if(arr == null)
15         {
16             throw new NullPointerException("数组不能为空");
17         }
18         if(index >= arr.length)
19         {
20             throw new ArrayIndexOutOfBoundsException("数组角标越界了");
21         }
22         if(index < 0)
23         {
24     
25             throw new FushuIndexException("角标不能为负数");
26         }
27         return arr[index];
28     }
29 }
30 
31 public class ExceptionDemo {
32     public static void main(String[] args) throws FushuIndexException
33     {
34         int[] arr = new int[3];
35         Demo demo = new Demo();
36         demo.method(arr,-1);
37     }
38 }

throw与throws的区别
  Ⅰ.throws使用在函数上;throw使用在函数内
  Ⅱ.throws抛出的是异常类,可以抛出多个,用逗号隔开;throw抛出的是异常类对象

 

异常处理

异常捕捉try-catch捕捉形式,这是可以对异常进行针对性处理的方式。

 1 //具体格式是:
 2 try
 3 {
 4     //需要被检测异常的代码。   
 5 }
 6 catch(异常类 变量)//该变量用于接收发生的异常对象
 7 {
 8     //处理异常的代码。
 9 }finally
10 {
11     //一定会被执行的代码。
12 }
 1 //实例
 2 class FushuIndexException extends Exception
 3 {
 4     FushuIndexException(){}
 5     FushuIndexException(String msg){
 6         super(msg);
 7     }
 8 }
 9 public class Demo {
10     public int method(int[] arr, int index) throws FushuIndexException
11     {
12         if(arr == null)
13         {
14             throw new NullPointerException("数组不能为空");
15         }
16         if(index >= arr.length)
17         {
18             throw new ArrayIndexOutOfBoundsException("数组角标越界了");
19         }
20         if(index < 0)
21         {
22             throw new FushuIndexException("角标不能为负数");
23         }
24         return arr[index];
25     }
26 }
27 public class ExceptionDemo {
28     public static void main(String[] args)
29     {
30         int[] arr = new int[3];
31         Demo demo = new Demo();
32         try
33         {
34             int num = demo.method(arr,-1);
35             System.out.println("num="+num);
36         }
37         catch (NullPointerException e)
38         {
39             System.out.println("string"+e.toString());
40         }
41         catch (FushuIndexException e)
42         {
43             //System.out.println("massage:"+e.getMessage());
44             //System.out.println("string"+e.toString());
45             e.printStackTrace();//jvm默认的异常处理机制
46             //System.out.println("负数角标异常!");
47         }
48         /*catch(Exception e)//多个catch时这个一定要放最后面
49         {
50 
51         }*/
52         System.out.println("over");
53     }
54 }
55 /*输出结果
56 com.exceptiondemo.www.FushuIndexException: 角标不能为负数
57     at com.exceptiondemo.www.Demo.method(Demo.java:23)
58     at com.exceptiondemo.www.ExceptionDemo.main(ExceptionDemo.java:10)
59 over
60 */

 

异常处理原则
  Ⅰ.函数内容如果抛出需要检测的异常,那么函数上必须要声明,否则必须在函数内用try-catch捕捉,否则编译失败.
  Ⅱ.如果调用到了声明异常的函数,要么try-carch要么throws,否则编译失败.
  Ⅲ.什么时候catch,什么时候throws呢?功能内容可以解决,用catch。解决不了,用throws告诉调用者,有调用者解决.
  Ⅳ.一个功能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性处理。内部又几个需要检测的异常,就抛几个,就catch几个.

 

异常的注意事项

  Ⅰ.子类在覆盖父类方法时,父类的方法如果抛出了异常。那么子类的方法只能抛出父类的异常或者该异常的子类。

 1 class A extends Excepion{}
 2 class B extends A{}
 3 class C extends Exception{}
 4 
 5 Exception
 6 |--A
 7     |--B
 8 |--C
 9 
10 class Father
11 {
12 void show throws A{}
13 }
14 class Son extends Father
15 {
16 void show throws A{} //也可以抛B,也可以不抛,不能抛C17 }

  Ⅱ.如果父类抛出多个异常,那么子类只能抛出父类异常的子集。

  注意:如果父类的方法没有抛出异常,那么子类覆盖时绝对不能抛,就只能try。

 

实例

1 //实例
 2 class FushuIndexException extends Exception
 3 {
 4     FushuIndexException(){}
 5     FushuIndexException(String msg){
 6         super(msg);
 7     }
 8 }
 9 public class Demo {
10     public int method(int[] arr, int index) throws FushuIndexException
11     {
12         if(arr == null)
13         {
14             throw new NullPointerException("数组不能为空");
15         }
16         if(index >= arr.length)
17         {
18             throw new ArrayIndexOutOfBoundsException("数组角标越界了");
19         }
20         if(index < 0)
21         {
22             throw new FushuIndexException("角标不能为负数");
23         }
24         return arr[index];
25     }
26 }
27 public class ExceptionDemo {
28     public static void main(String[] args)
29     {
30         int[] arr = new int[3];
31         Demo demo = new Demo();
32         try
33         {
34             int num = demo.method(arr,-1);
35             System.out.println("num="+num);
36         }
37         catch (NullPointerException e)
38         {
39             System.out.println("string"+e.toString());
40         }
41         catch (FushuIndexException e)
42         {
43             //System.out.println("massage:"+e.getMessage());
44             //System.out.println("string"+e.toString());
45             e.printStackTrace();//jvm默认的异常处理机制
46             //System.out.println("负数角标异常!");
47         }
48         /*catch(Exception e)//多个catch时这个一定要放最后面
49         {
50 
51         }*/
52         System.out.println("over");
53     }
54 }
55 /*输出结果
56 com.exceptiondemo.www.FushuIndexException: 角标不能为负数
57     at com.exceptiondemo.www.Demo.method(Demo.java:23)
58     at com.exceptiondemo.www.ExceptionDemo.main(ExceptionDemo.java:10)
59 over
60 */

 

 1 //实例
 2 public class Demo {
 3     public int show(int index)
 4     {
 5         if(index<0)
 6             throw new ArrayIndexOutOfBoundsException("越界啦!!");
 7         return index;
 8     }
 9 }
10 
11 public class ExceptionDemo {
12     public static void main(String[] args)
13     {
14         Demo d = new Demo();
15         try
16         {
17             int num = d.show(-1);
18             System.out.println(num);
19         }
20         catch(ArrayIndexOutOfBoundsException e)
21         {
22             System.out.println(e.toString());
23             //return;//over这个关键字没有被输出
24             //System.exit(0);//退出JVM,finally与over关键字没有被输出
25         }
26         finally
27         {
28             System.out.println("finally");
29         }
30         System.out.println("over");
31     }
32 }

 

  1 //异常的应用:毕老师用电脑上课,问题分析:比如电脑蓝屏,冒烟
  2 class LanPingException extends Exception
  3 {
  4     LanPingException(String msg)
  5     {
  6         super(msg);
  7     }
  8 }
  9 
 10 class MaoYanException extends Exception
 11 {
 12     MaoYanException(String msg)
 13     {
 14         super(msg);
 15     }
 16 }
 17 
 18 class NoPlanException extends Exception
 19 {
 20     NoplanException(String msg)
 21     {
 22         super(msg);
 23     }
 24 }
 25 
 26 class Computer
 27 {
 28     private int state = 0;
 29     public void run()throws LanPingException,MaoYanException
 30     {
 31         if(state == 1)
 32         {
 33            throw new LanPingException("电脑蓝屏");
 34         }
 35         if(state == 2)
 36         {
 37             throw new MaoYanException("电脑冒烟");
 38         }
 39         System.out.println("电脑运行");
 40     }
 41     
 42     public void reset()
 43     {
 44         System.out.println("电脑重新启动");
 45     }
 46 }
 47 
 48 class Teacher
 49 {
 50     private String name;
 51     private Computer comp;
 52     
 53     Teacher(String name)
 54     {
 55         this.name = name;
 56         comp = new Computer();
 57     }
 58     
 59     public void prelect()throws NoPlanException
 60     {
 61         try
 62         {
 63             this.comp.run();
 64             System.out.println("讲课");
 65         }
 66         catch(LanPingException e)
 67         {
 68             System.out.println(e.toString());
 69             this.comp.reset();
 70             prelect();
 71         }
 72         catch(MaoYanException e)
 73         {
 74             System.out.println(e.toString());
 75             test();
 76             //可以对电脑进行维修
 77             //throw e;
 78             throw new NoPlanException("课时进度无法完成," + e.getMessage());
 79         }
 80     }
 81     public void test()
 82     {
 83         System.out.println("大家练习");
 84     }
 85 }
 86 
 87 class Exception
 88 {
 89     public static void main(String[] args)
 90     {
 91         Teacher t = new Teacher("毕老师");
 92         try
 93         {
 94             t.prelect();
 95         }
 96         catch(NoPlanException e)
 97         {
 98             System.out.println(e.toSring"........");
 99             System.out.println("换人");
100         }
101     }
102 }

 

posted on 2020-03-05 12:55  白客C  阅读(194)  评论(0编辑  收藏  举报