黑马程序员_面向对象(三)_异常

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

 异常:

 

 

异常:就是程序在运行时出现不正常情况。

 

 

异常由来:问题也是现实生活中一个具体的事物,也可以通过java的类的形式进行描述。并封装成对象。其

 

 

实就是java对不正常情况进行描述后的对象体现。

 

 

对于问题的划分:

 

 

两种:一种是严重的问题,一种非严重的问题。

 

 

对于严重的,java通过Error类进行描述。对于Error一般不编写针对性的代码对其进行处理。

 

 

对与非严重的,java通过Exception类进行描述。对于Exception可以使用针对性的处理方式进行处理。

 

 

异常的处理:

 

 

try

{

    //需要被检测的代码;

}

catch(异常类变量)

{

    //处理异常的代码;(处理方式)

}

finally

{

    //一定会执行的语句;

}

 

 

对多异常的处理:

 

 

1、声明异常时,建议声明更为具体的异常。这样处理的可以更具体。

 

 

2、对方声明几个异常,就对应有几个catch块。不要定义多余的catch块。如果多个catch块中的异常出现继

 

 

承关系,父类异常catch块放在最下面。

 

 

Java代码   收藏代码
  1. class Demo{  
  2.       
  3.     //在功能上通过throws的关键字声明了该功能有可能会出现问题  
  4.     int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException {  
  5.   
  6.         int[] arr = new int[a];  
  7.   
  8.         System.out.println(arr[4]);  
  9.   
  10.         return a/b;  
  11.     }  
  12. }  
  13.   
  14.   
  15. class  ExceptionDemo2 {  
  16.       
  17.     public static void main(String[] args) //throws Exception  
  18.     {  
  19.         Demo d = new Demo();  
  20.         try  
  21.         {  
  22.             int x = d.div(5,0);  
  23.             System.out.println("x="+x);  
  24.         }  
  25.   
  26.         catch (ArithmeticException e)  
  27.         {  
  28.             System.out.println(e.toString());  
  29.             System.out.println("被零除了!!");  
  30.   
  31.         }  
  32.         catch (ArrayIndexOutOfBoundsException e)  
  33.         {  
  34.             System.out.println(e.toString());  
  35.             System.out.println("角标越界啦!!");  
  36.         }  
  37.         catch(Exception e)//父类要放在最后  
  38.         {  
  39.             System.out.println("hahah:"+e.toString());  
  40.         }  
  41.   
  42.         System.out.println("over");  
  43.   
  44.     }  
  45. }  

 

 

当在函数内部出现了throw抛出异常对象,那么就必须要给对应的处理动作。
 

要么在内部try catch处理。
 

要么在函数上声明让调用者处理。
 

 

一般情况在,函数内出现异常,函数上需要声明。

 

 

 

如何定义异常信息:

 


因为父类中已经把异常信息的操作都完成了。

 


所以子类只要在构造时,将异常信息传递给父类通过super语句。

 


那么就可以直接通过getMessage方法获取自定义的异常信息。

 

 

继承Exception原因:

 


异常体系有一个特点:因为异常类和异常对象都被抛出。

 


他们都具备可抛性。这个可抛性是Throwable这个体系中独有特点。

 

 

throws和throw的区别:

 


throws使用在函数上。

 


throw使用在函数内。

 

 

throws后面跟的异常类。可以跟多个。用逗号隔开。

 


throw后跟的是异常对象。
 

 

Exceptoin中有一个特殊的子类异常RuntimeException 运行时异常:

 

 

如果在函数内容抛出该异常,函数上可以不用声明,编译一样通过。

 

 

如果在函数上声明了该异常。调用者可以不用进行处理。编译一样通过;

 

 

之所以不用在函数声明,是因为不需要让调用者处理。

 


当该异常发生,希望程序停止。因为在运行时,出现了无法继续运算的情况,希望停止程序后,对代码

 

 

进行修正。

 

 

对于异常分两种:

 


1、编译时被检测的异常。


 
2、编译时不被检测的异常(运行时异常。RuntimeException以及其子类)

 

异常练习:

Java代码   收藏代码
  1. //蓝屏异常  
  2. class LanPingException extends Exception  
  3. {  
  4.     LanPingException(String message)  
  5.     {  
  6.         super(message);  
  7.     }  
  8. }  
  9. //冒烟异常  
  10. class MaoYanException extends Exception  
  11. {  
  12.     MaoYanException(String message)  
  13.     {  
  14.         super(message);  
  15.     }  
  16. }  
  17.   
  18. //无法上课异常  
  19. class NoPlanException extends Exception  
  20. {  
  21.     NoPlanException(String msg)  
  22.     {  
  23.         super(msg);  
  24.     }  
  25. }  
  26.   
  27. class Computer  
  28. {  
  29.     private int state = 3;//电脑运行状态  
  30.     public void run()throws LanPingException,MaoYanException  
  31.     {  
  32.         if(state==2)  
  33.             throw new LanPingException("蓝屏了");  
  34.         if(state==3)  
  35.             throw new MaoYanException("冒烟了");  
  36.   
  37.         System.out.println("电脑运行");  
  38.     }  
  39.     public void reset()//重启电脑方法  
  40.     {  
  41.         state = 1;  
  42.         System.out.println("电脑重启");  
  43.           
  44.     }  
  45. }  
  46.   
  47. class Teacher  
  48. {  
  49.     private String name;  
  50.     private Computer cmpt;  
  51.   
  52.     Teacher(String name)  
  53.     {  
  54.         this.name = name;  
  55.         cmpt = new Computer();  
  56.   
  57.     }  
  58.   
  59.     //老师讲课方法  
  60.     public void prelect()throws NoPlanException  
  61.     {  
  62.         try  
  63.         {  
  64.             cmpt.run();//老师启动电脑           
  65.         }  
  66.         catch (LanPingException e)  
  67.         {  
  68.             cmpt.reset();//老师重启电脑  
  69.         }  
  70.         catch (MaoYanException e)  
  71.         {  
  72.               
  73.             test();  
  74.             throw new NoPlanException("课时无法继续"+e.getMessage());  
  75.               
  76.         }  
  77.         System.out.println("讲课");  
  78.     }  
  79.     public void test()  
  80.     {  
  81.         System.out.println("练习");  
  82.     }  
  83.   
  84. }  
  85.   
  86.   
  87.   
  88. class ExceptionTest   
  89. {  
  90.     public static void main(String[] args)   
  91.     {  
  92.         Teacher t = new Teacher("毕老师");  
  93.         try  
  94.         {  
  95.             t.prelect();  
  96.         }  
  97.         catch (NoPlanException e)  
  98.         {  
  99.             System.out.println(e.toString());  
  100.             System.out.println("换老师或者放假");  
  101.         }  
  102.           
  103.     }  
  104. }  

 

 

finally代码块:定义一定执行的代码。通常用于关闭资源。

 

//第一个格式:

try

{

   

}

catch ()

{

}

 

//第二个格式:

try

{

   

}

catch ()

{

}

finally

{

 

}

 

//第三个格式:

try

{

   

}

finally

{

}

 

 

 

异常在子父类覆盖中的体现:

 


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

 

 

子类。

 

 

2、如果父类方法抛出多个异常,那么子类在覆盖该方法时,只能抛出父类异常的子集。

 


3、如果父类或者接口的方法中没有异常抛出,那么子类在覆盖方法时,也不可以抛出异常。如果子

 

 

类方法发生了异常。就必须要进行try处理。绝对不能抛。

 

 

异常练习:有一个圆形和长方形。都可以获取面积,对于面积如果出现非法的数值,视为是获取面积出现问

 

 

题。问题通过异常来表示。

 

Java代码   收藏代码
  1. class NoValueException extends RuntimeException//自定义异常  
  2. {  
  3.     NoValueException(String message)  
  4.     {  
  5.         super(message);  
  6.     }  
  7. }  
  8.   
  9. interface Shape//获取面积的接口  
  10. {  
  11.     void getArea();  
  12. }  
  13.   
  14. class Rec implements Shape//长方形  
  15. {  
  16.     private int len,wid;  
  17.   
  18.     Rec(int len ,int wid)//throws NoValueException运行期异常可以不声明  
  19.     {  
  20.         if(len<=0 || wid<=0)//判断数据是否符合要求  
  21.             throw new NoValueException("出现非法值");  
  22.   
  23.         this.len = len;  
  24.         this.wid = wid;  
  25.     }  
  26.   
  27.     public void getArea()  
  28.     {  
  29.         System.out.println(len*wid);  
  30.     }  
  31. }  
  32.   
  33.   
  34. class Circle implements Shape//圆形  
  35. {  
  36.     private int radius;  
  37.     public static final double PI = 3.14;//圆周率常量  
  38.   
  39.     Circle(int radius)  
  40.     {  
  41.         if(radius<=0)//判断数据  
  42.             throw new NoValueException("非法");  
  43.         this.radius = radius;  
  44.     }  
  45.   
  46.     public void getArea()  
  47.     {  
  48.         System.out.println(radius*radius*PI);  
  49.     }  
  50. }  
  51.   
  52.   
  53.   
  54.   
  55. class  ExceptionTest1  
  56. {  
  57.     public static void main(String[] args)   
  58.     {     
  59.           
  60.         Rec r = new Rec(3,4);  
  61.         r.getArea();  
  62.   
  63.         Circle c = new Circle(-8);//负数传入导致异常  
  64.   
  65.         System.out.println("over");  
  66.       
  67.     }  
  68. }  

---------------------- <a href="http://www.itheima.com"target="blank">ASP.Net+Unity开发</a>、<a href="http://www.itheima.com"target="blank">.Net培训</a>、期待与您交流! ----------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值