zoukankan      html  css  js  c++  java
  • 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 }

      代码运行:

      

  • 相关阅读:
    【NOIP 2003】 加分二叉树
    【POJ 1655】 Balancing Act
    【HDU 3613】Best Reward
    【POJ 3461】 Oulipo
    【POJ 2752】 Seek the Name, Seek the Fame
    【POJ 1961】 Period
    【POJ 2406】 Power Strings
    BZOJ3028 食物(生成函数)
    BZOJ5372 PKUSC2018神仙的游戏(NTT)
    BZOJ4836 二元运算(分治FFT)
  • 原文地址:https://www.cnblogs.com/zhuxr/p/9046167.html
Copyright © 2011-2022 走看看