异常

来源:互联网 发布:打开淘宝商品链接 编辑:程序博客网 时间:2024/05/17 08:07
异常:是在运行时期发生的不正常情况。
 
在java中用于类的形式对不正常情况进行了描述和封装对象。
 
描述不正常的情况的类,就称为异常类。
 

以前叫做正常流程代码和问题处理代码相结合。
 
现在讲正常流程代码和问题处理代码分离,提高阅读性。
 

 其实异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行了描述。
 
 不同的问题用不同的类进行具体的描述,比如角标越界、空指针等

问题很多,意味着描述的类也很多,将其共性向上抽取就形成了异常体系。
 

最终问题(不正常情况)就分成了两大类。
 
Throwable:无论是error还是异常都是问题,问题发生就应该可以抛出,让调用者知道并处理。
 
该体系的特点就在于Throwable及其所有的子类都具有可抛性。
 
可抛性到底指什么呢?   怎么体现可抛性呢?
 
其实就是通过两个关键字来体现的。
 
throws和throw,凡是可以被这两个关键字所操作的类和对象都具备可抛性。  
 
   
|---1、一般不可处理的。error
 
特点:是由jvm抛出的严重性的问题,。
 
这种问题发生一般不针对性处理,直接修改程序。
 
|---2、可以处理的。exception
 
该体系的特点:
 
子类的后缀名都是用其父类名作为后缀,阅读性很强。
 
异常对象的抛出:

[java] view plaincopyprint?
  1. class Demo{      
  2.     public int method(int[]arr,int index){      
  3.         if(arr==null)      
  4.             throw new NullPointerException("数组的引用不能为空!");      
  5.         if(index >= arr.length)      
  6.             throw new ArrayIndexOutOfBoundsException("数组的角标越界啦,哥们你是不是疯了:"+index);      
  7.         if(index<0)      
  8.             throw new ArrayIndexOutOfBoundsException("数组的角标不能为负数:"+index);      
  9.         return arr[index];      
  10.     }      
  11. }      
  12.     
  13.     
  14. class ExceptionDemo{      
  15.     public static void main(String[] args){      
  16.         int[] arr = new int[3];      
  17.         Demo d = new Demo();      
  18.         int num = d.method(arr, 4);      
  19.     }      
  20. }      

-----------------------------------------------------
 
自定义异常的抛出,throws
 
对于角标时正数不存在,可以用角标越界表示。
 
对于负数为角标的情况,准备用负数角标异常来表示。


负数角标这种异常在java中并没有定义过。
 
那就按照java异常的创建思想,面向对象将负数角标进行自定义描述。并封装成对象。

这种自定义的问题描述成为自定义异常。


注意:如果让一个类成为异常类,必须要继承异常依稀,因为只有成为异常体系的子类,才有资格具备可抛性。
 
才可以被两个关键字所操作,throws  throw

如果java编译器先检查语法错误,然后检查基本的逻辑错误。
[java] view plaincopyprint?
  1. class FuShuIndexException extends Exception{      
  2.     FuShuIndexException(){}      
  3.     FuShuIndexException(String msg){      
  4.         super(msg);      
  5.     }      
  6. }      
  7.     
  8.     
  9. class Demo{      
  10.     public int method(int[] arr,int index)throws FuShuIndexException{      
  11.         if(index<0)      
  12.             throw new FuShuIndexException("角标不能为负数");      
  13.         return arr[index];      
  14.     }      
  15. }      
  16.     
  17.     
  18. public class ExceptionDemo {      
  19.     public static void main(String[] args) throws FuShuIndexException {      
  20.         int[] arr = new int[3];      
  21.         Demo d = new Demo();      
  22.         int num = d.method(arr, -2);      
  23.     }      
  24. }      

-----------------------------------------------------
 
编译时检测异常和运行时异常的区别
 
异常的分类
 
1.编译时被检测异常:只要是Exception和其子类都是,除了特殊子类RuntimeException
 
这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式。
 
这样的问题都可以针对性的处理。
 

2.编译时不检测异常(运行时异常RuntimeException):就是Exception中的RuntimeException和其子类。
 
这种问题的发生,无法让功能继续,运算无法进行,更多是因为调用者的原因导致的,或者引发了内部状态的改变导致的。
 
这种问题一般不处理,直接编译通过,在运行时,让调用者调用时的程序强制停止。让调用者对代码进行修正。
 
RuntimeException是那些可能在Java虚拟机正常运行期间抛出的异常的超类。

所以自定义异常是要么继承Exception要么继承RuntimeException.
 


throws和throw的区别。
 
1.throws使用在函数上,throw使用在函数内。
 
2.throws抛出的是异常类,可以抛多个,中间用逗号隔开。throw抛出的是异常对象。
 

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

具体格式是:
 
try{
 
//需要被检测异常的代码。
 
}
 
catch(异常类  变量){//该变量用于接收发生的异常对象
 
//处理异常的代码。
 
}
 
finally{
 
//一定会被执行的代码。
 
}
 
如果问题能处理就try catch 不能处理就throws


 

[java] view plaincopyprint?
  1. class FuShuIndexException extends Exception{      
  2.     FuShuIndexException(){}      
  3.     FuShuIndexException(String msg){      
  4.         super(msg);      
  5.     }      
  6. }      
  7. class Demo{      
  8.     public int method(int[] arr,int index)throws FuShuIndexException{      
  9.         if(index<0)      
  10.             throw new FuShuIndexException("角标不能为负数");      
  11.         return arr[index];      
  12.     }      
  13. }      
  14. public class ExceptionDemo {      
  15.     public static void main(String[] args){      
  16.         int[] arr = new int[3];      
  17.         Demo d = new Demo();      
  18.         try {      
  19.             int num = d.method(arr, -2);      
  20.         } catch (FuShuIndexException e) {//抛什么就catch什么      
  21.             System.out.println("message:"+e.getMessage());      
  22.             System.out.println(e);      
  23.             e.getStackTrace();//虚拟机的默认异常处理机制。      
  24.             System.out.println("负数角标异常!!!");      
  25.         }      
  26.         System.out.println("over");//程序可以继续运行。      
  27.     }      
  28. }      

打印结果:  
message:角标不能为负数  
负数角标异常!!!  
over  

-----------------------------------------------------
 
多catch情况。
[java] view plaincopyprint?
  1. class FuShuIndexException extends Exception{      
  2.     FuShuIndexException(){}      
  3.     FuShuIndexException(String msg){      
  4.         super(msg);      
  5.     }      
  6. }      
  7. class Demo{      
  8.     public int method(int[] arr,int index)throws FuShuIndexException{      
  9.         if(arr==null)      
  10.             throw new NullPointerException("没有任何数组实体");      
  11.         if(index<0)      
  12.             throw new FuShuIndexException("角标不能为负数");      
  13.         return arr[index];      
  14.     }      
  15. }      
  16. public class ExceptionDemo {      
  17.     public static void main(String[] args){      
  18.         int[] arr = new int[3];      
  19.         Demo d = new Demo();      
  20.         try {      
  21.             int num = d.method(null, -2);      
  22.         }      
  23.         catch (NullPointerException e){      
  24.             System.out.println(e.toString());      
  25.         }      
  26.         catch (FuShuIndexException e) {      
  27.             System.out.println(e.toString());      
  28.         }      
  29.         catch(Exception e){//多catch情况,存在父类catch情况,一定要把父类放在最下面。      
  30.                   //面试中可能会出现。      
  31.         }      
  32.         System.out.println("over");//程序可以继续运行。      
  33.     }      
  34. }      

打印结果:  
java.lang.NullPointerException: 没有任何数组实体  
over  

-----------------------------------------------------------------------
异常处理的原则:
 
1.函数内部如果抛出需要检测到的异常,那么函数上必须要声明。
 
  否则必须在函数内用try catch,否则编译失败。
 
2.如果调用到了声明异常的函数,要么try catch要么throws,否则编译失败。

3.什么时候catch什么时候throw呢?
 
功能内部可以解决,用catch。解决不了用throws告诉调用者,由调用者解决。
 
4.一个能如果抛出了多个异常,那么调用时,必须有对应多个catch进行针对性的处理。
 
内部有几个需要检测的异常就抛几个异常,抛出几个就catch几个。
 


异常  finally代码块

[java] view plaincopyprint?
  1. class Demo{      
  2.     public int show(int index){      
  3.         if(index<0)      
  4.             throw new ArrayIndexOutOfBoundsException("角标越界啦");      
  5.         int[] arr = new int[3];      
  6.         return arr[index];      
  7.     }      
  8. }      
  9. class ExceptionDemo{      
  10.     public static void main(String[] args){      
  11.         Demo d = new Demo();      
  12.         try{      
  13.             int num = d.show(-1);      
  14.             System.out.println(num);      
  15.         }      
  16.         catch(ArrayIndexOutOfBoundsException e){      
  17.             System.out.println(e.toString());      
  18.             //System.exit(0);退出jvm      
  19.         }      
  20.         finally{//一定会执行的代码。通常用于关闭(释放)资源。      
  21.             System.out.println("finally");      
  22.         }      
  23.         System.out.println("over");      
  24.     }      
  25. }      
  26. //连接数据库      
  27. //查询。      
  28. //关闭连接。      
  29. try catch finally 代码块组合特点:      
  30. 1.try catch finally      
  31. 2.try catch(多个) 当没有必要资源需要释放时,可以不定义finally      
  32. 3.try finally:异常无法直接catch处理,但是资源需要关闭。      
  33. catch就没处理,没catch就要声明。      
  34.     
  35.     
  36. void show() throws Exception{      
  37.     try{      
  38.         //开启资源      
  39.         throw new Exception();      
  40.     }      
  41.     finally{      
  42.         //关闭资源      
  43.     }      
  44. }      

------------------------------------------------------
 
异常应用:异常转换
 
毕老师用电脑上课。
 
问题领域中涉及两个对象。
 
毕老师,电脑。
 
可能会出现的问题。
 
比如电脑蓝屏啦,冒烟啦。

[java] view plaincopyprint?
  1. class LanPingException extends Exception{      
  2.     LanPingException(String msg){      
  3.     super(msg);      
  4.     }      
  5. }      
  6. class MaoYanException extends Exception{      
  7.     MaoYanException(String msg){      
  8.     super(msg);      
  9.     }      
  10. }      
  11. class NoPlanException extends Exception{      
  12.     NoPlanException(String msg){      
  13.     super(msg);      
  14.     }      
  15. }      
  16. class Computer {      
  17.     private int state = 0;      
  18.     public void run()throws LanPingException,MaoYanException{      
  19.         if(state ==1)      
  20.             throw new LanPingException("电脑蓝屏啦");      
  21.         if(state ==2)      
  22.             throw new MaoYanException("电脑冒烟啦");      
  23.         System.out.println("电脑运行");      
  24.     }      
  25.     public void reset(){      
  26.         state = 0;      
  27.         System.out.println("电脑重启");      
  28.     }      
  29. }      
  30. class Teacher{      
  31.     private String name;      
  32.     private Computer comp;      
  33.     Teacher(String name){      
  34.         this.name = name;      
  35.         comp = new Computer();      
  36.     }      
  37.     public void prelect()throws NoPlanException{      
  38.         try {      
  39.             comp.run();      
  40.             System.out.println(name+"讲课");      
  41.         } catch (LanPingException e) {      
  42.             System.out.println(e.toString());      
  43.             comp.reset();      
  44.             prelect();      
  45.         } catch (MaoYanException e) {       
  46.             System.out.println(e.toString());      
  47.             test();      
  48.             //可以对电脑进行维修      
  49.             //throw e;      
  50.             throw new NoPlanException("课时无法完成");      
  51.         }      
  52.         System.out.println("讲课");      
  53.     }      
  54.     public void test(){      
  55.         System.out.println("大家练习");      
  56.     }      
  57. }      
  58. class ExceptionTest{      
  59.     public static void main(String[] args){      
  60.         Teacher t = new Teacher("毕老师");      
  61.         try {      
  62.             t.prelect();      
  63.         } catch (NoPlanException e) {      
  64.             System.out.println(".....");      
  65.         }      
  66.     }      
  67. }      

异常的转换。没有必要暴露的异常就不要暴露出去。  
class NoAddException extendsException{}  
    void addData(Data d)throws NoAddException{  
        连接数据库  
        try{  
            添加数据。出现异常 SQLException();  
        }  
        catch(SQLException e){  
            //处理代码。  
            throw new NoAddException();  
        }  
        finally{  
            //关闭数据库。  
        }  
  
    }    
}  


-------------------------------------------------------------------------------------------------------------------------------------------------------------
异常的注意事项。
 
1.子类在覆盖父类方法时,父类的方法如果抛出了异常,那么子类的方法只能抛出父类的异常或者该异常的子类。
 
2.如果父类抛出多个异常,那么子类只能抛出父类异常的子集。
 
简单说:子类覆盖父类只能抛出父类的异常或者其异常子类或者子集。
原创粉丝点击