1四、Java的異常

異常:指的是程序在執行過程當中,出現非正常的狀況,最終會致使JVM的非正常中止。數組

當出現異常時:JVM會作兩件事:一、JVM會根據異常產生的緣由建立一個異常對象,這個異常對象包含了異常產生的緣由(內容,緣由,位置)函數

                                             二、沒有異常處理的邏輯,那麼JVM就會把異常對象拋出給方法的調用者來處理這個異常。字體

                                             三、若是調用方法也沒有異常的處理邏輯,把異常拋給JVM處理日誌

                                             四、JVM接收到這個異常對象,作兩件事:把異常對象(內容,緣由,位置)以紅色的字體打印在控制檯,而且終止執行程序對象

異常處理的五個關鍵字:try,catch,finally,throw,throws索引

 

throw:可使用throw關鍵字在指定的方法中拋出指定的異常get

使用格式:throw new XXXException(「異常產生的緣由」);io

注意:一、throw關鍵字必須寫在方法的內部編譯

         二、throw關鍵字後邊new的對象必須是Exception或者Exception的子類對象class

         三、throw關鍵字拋出指定的異常對象,咱們就必須處理這個異常對象

                    throw關鍵字後邊建立的是運行期異常,咱們能夠不處理,交給JVM處理(打印異常,中斷程序)

                    throw關鍵字後邊建立的是編譯期異常,咱們必須處理這個異常,要麼throws或者try....catch

示例代碼:

public class Demo {
    public static void main(String[] args) {
        int [] arr=new int[10];
        System.out.println(arr.length);
        System.out.println(getElement(arr,2));
    }

    public static int getElement(int[] arr,int index){
        if (arr==null){
            throw new NullPointerException("數組爲空");
        }

        if (index<0||index>(arr.length-1)){
            throw new ArrayIndexOutOfBoundsException("索引超出");
        }

        return arr[index];
    }
}

throws:異常處理的第一種方式,交給別人處理,可使用throws關鍵字處理異常對象,會把異常對象聲明拋出給方法的調用者處理(本身不處理,交給別人處理),最終交給JVM處理,,中斷程序

throws的使用格式:修飾符 返回值類型 方法名(參數列表)throws AAAexception,BBBException{

                                throw new AAAException (「產生緣由」);

                                throw new BBBException ("產生緣由");

}

注意:一、throws關鍵字必須寫在方法的聲明處

         二、throws關鍵字後邊聲明的異常必須是Exception或者是Exception的子類

         三、方法內部若是拋出了多個異常對象,那麼throws後邊必須也聲明多個異常

                       若是拋出的多個異常對象有子父類關係,那麼直接聲明父類異常便可

         四、調用了一個聲明拋出異常的方法,咱們就必須的處理聲明的異常

                        要麼繼續使用throws聲明拋出,交給調用者處理,最終交給JVM

                        要麼try....catch本身處理異常

public class Demo {
    public static void main(String[] args)throws NullPointerException,ArrayIndexOutOfBoundsException {
        int [] arr=new int[10];
        System.out.println(arr.length);
        System.out.println(getElement(arr,11));
    }

    public static int getElement(int[] arr,int index) throws NullPointerException,ArrayIndexOutOfBoundsException{
        if (arr==null){
            throw new NullPointerException("數組爲空");
        }

        if (index<0||index>(arr.length-1)){
            throw new ArrayIndexOutOfBoundsException("索引超出");
        }

        return arr[index];
    }
}

try...catch:異常處理的第二中方式,捕獲異常,本身處理異常

格式:

try{

                可能產生異常的代碼

}catch(異常變量){

                異常處理的邏輯,通常在工做中,會把異常的信息記錄到一個日誌中

}catch(異常變量){}

注意:一、try中可能會拋出多個異常對象,那麼就可使用多個catch來處理這些異常

         二、若是try中產生了異常,那麼就會執行catch中的異常處理邏輯,執行完畢catch中的處理邏輯,繼續執行try...catch以後的代碼

         三、若是try中沒有產生異常,那麼就不會執行catch中的異常的處理邏輯,執行完try中的代碼,繼續執行try...catch以後的代碼

直接對上面的代碼的主函數作下面修改:

     

public class Demo {
    public static void main(String[] args){
        int [] arr=new int[10];
        System.out.println(arr.length);
        getElement(arr,11);
    }

    public static void getElement(int[] arr,int index) {
        try{
            System.out.println(arr[index]);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

throwable:異常的超類,三種處理異常的方法:一、String getMessage(),異常的簡單描述

                                                              二、String toString(),異常的詳細描述

                                                              三、void printStackTrace(),JVM打印異常對象默認調用此方法,最詳細

 

finally:有一些特定的代碼不管異常是否發生,都須要執行。另外,由於異常會引起程序跳轉,致使有些語句執行不到,而finally就是解決這個問題的,在finally代碼塊中存放的代碼都是必定會被執行的

格式:在try...catch後面添加finally 

public class Demo {
    public static void main(String[] args){
        int [] arr=new int[10];
        System.out.println(arr.length);
        getElement(arr,11);
    }

    public static void getElement(int[] arr,int index) {
        try{
            System.out.println(arr[index]);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            System.out.println("我必定有");
        }

    }
}

多個異常捕獲處理方式:一、多個異常分別處理,一個try對應一個catch

                                 二、多個異常一次捕獲,屢次處理,一個try對應多個catch,若是異常變量存在子父類關係,子類必須在上邊

                                 三、多個異常一次捕獲,一次處理,只有一個try對應一個catch,catch裏的異常變量是全部異常的父類

相關文章
相關標籤/搜索