Java面向对象之异常(throw与throws)

 

一、基础概念

  1、throw和throws的区别:

   位置不同:throws用在函数上,后面跟的是异常类,可以跟多个。

         throw用在函数内,后面跟的是异常对象。

   功能不同:throws用来声明异常,让调用者知道该功能有可能出现的问题,并由调用者给出预先的处理方式。

         throw抛出具体问题的对象。语句执行到throw功能就结束了,跳转到调用者。并将具体的问题对象抛给调用者。

   注意:throw语句独立存在,下面不要定义其他语句。因为执行不到throw下面的语句。

  2、异常体系的特点:  类以及对象都具备可抛性,可以被throws和throw所操作。

  3、异常的原则:
   一般情况:(编译时会检测到异常)

    功能内部有异常throw抛出,功能上一定要throws声明。功能内部有异常throw抛出,功能上一定要throws声明。(内部抛什么,功能上就声明什么。)

    声明的目的就是为了让调用者处理,如果调用者不处理,编译失败。

   特殊情况:(编译时不进行检查)
    当函数内通过throw抛出了RuntimeException时,函数上可以不用throws声明。

    不声明是不让调用者处理。运行时发生异常,程序会停止,对代码进行修改。

  4.Exception分两种:

    编译时会被检测的异常。

    运行时异常(编译时不检测)RuntimeException

二、编译时会被检测的异常代码:

 1 class Demo
 2 {
 3     int div(int a,int b)throws Exception//声明异常Exception
 4     {
 5         if(b==0)
 6             throw new Exception("异常信息:除数不能为0");//抛出具体问题,编译时进行检测。
 7         return a/b;
 8     }
 9 }
10     
11 class ExceptionDemo1
12 {
13     public static void main (String[] arge)
14     {
15         Demo d = new Demo();
16         
17         //对异常进行处理
18         try
19         {
20             int num = d.div(4,0);
21             System.out.println("num="+num);
22         }
23         catch(Exception e)
24         {
25             //处理这个对象,可以使用该对象的方法。
26             System.out.println("处理异常的代码:除数不能为0");
27             System.out.println(e.getMessage());//异常信息
28             System.out.println(e.toString());//异常名称+异常信息
29             e.printStackTrace();//异常名字+异常信息+位置。jvm默认处理收到异常就是调用这个方法。将信息显示在屏幕上。
30         }
31         System.out.println("over");
32     }
33 }

  运行代码:

  

三、编译时不检测,运行异常程序停止代码

    ArithemticException异常 属于 RuntimeException异常,所以函数上也可以不用throws声明。

 1 class Demo
 2 {
 3     int div(int a,int b)
 4     {
 5         if(b==0)
 6             throw new ArithmeticException("异常信息:除数不能为0");//抛出具体问题,编译时不检测
 7         return a/b;
 8     }
 9 }
10     
11 class ExceptionDemo1
12 {
13     public static void main (String[] arge)
14     {
15         Demo d = new Demo();
16         
17         int num =d.div(4,0);
18         System.out.println("num="+num);
19         System.out.println("over");//这句话不能被执行了
20     }
21 }

  代码运行:

  

 

    NullPointerExceptionException异常  和 ArrayIndexOutOfBoundsException异常 都 属于 RuntimeException异常,所以函数上也可以不用throws声明。

 1 class ExceptionDemo1 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         int[] arr = new int[3];
 6         //arr = null;
 7         System.out.println("element:"+getElement(arr,-2));
 8     }
 9     //定义一个功能。返回一个整型数组中指定位置的元素。
10     public static int getElement(int[] arr,int index)
11     {
12         if(arr==null)
13             throw new NullPointerException("数组实体不存在");
14 
15         if(index<0 || index>=arr.length)
16         {
17             throw new ArrayIndexOutOfBoundsException(index+",角标不存在");
18         }
19         System.out.println("element:"+getElement(arr,-2));
20         int element = arr[index];
21         return element;
22     }
23 }

  代码运行:

  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值