博客 / 詳情

返回

Java異常類型及使用

Java異常類型及處理

前言: 異常指的是程序在執行過程中,出現了非正常情況,導致了javajvm停止停止。

異常結構為

Throwable 為頂級父類

  • 子類Error為嚴重報錯 ,
  • 子類Exception就是我們所説的異常

異常處理的關鍵字

java中處理異常的有五個關鍵字: try、catch 、finally 、 throw 、throws

throw拋出異常 , thorws聲明異常 , 捕獲異常 try_catch


throw

public class SegmentFault {
    public static void main(String[] args) {

        /**
         *  throw 拋出異常
         *    格式 - throw new 異常類名(參數);
         * */

        // 創建一個數組
        int [] arr = { 2, 4, 56 ,5};
        // 根據索引找到對應的元素
        int index = 4;
        int element = getElement(arr,index);
        System.out.println(element);
        System.out.println("owo"); // 運行錯誤 無法繼續
    }
        /** throw 拋出異常 提醒你必須處理  */
    public static int getElement(int [] arr, int index){
        // 判斷數組索引是否越界
        if (index < 0  || index > arr.length -1){
            /**
             * 條件滿足越界 當執行到throw拋出異常後就無法運行,結束方法並且提示
             * */
            throw new ArrayIndexOutOfBoundsException("數組下標越界異常");
        }
        int element = arr[index];
        return element;
    }
}    

異常結果為

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 數組下標越界異常


throws

public class SegmentFault{
    public static void main(String [] args){
    
        read("a.txt");
        
    }
      public static void read(String path) throws FileNotFoundException, IOException {
        if (!path.equals("a.txt")){  // 如果沒有a.txt
            // 如果不是 a.txt 該文件不存在 是一個錯誤 也就是異常 throw
            throw new FileNotFoundException("文件不存在");
        }
        if (!path.equals("b.txt")){
            throw new IOException("文件不存在");
        }
    }
    
}

異常結果為

Exception in thread "main" java.io.IOException: 文件不存在


try、catch、finally + Throwable中的常用方法。

Throwable常用方法如下
printStackTrace() : *打印異常詳細信息。
getMessage() : 獲取異常原因。
toString(): 獲取異常類型及描述信息。


public class Demo03 {
    public static void main(String[] args) {

        /**
         *  try- catch  捕獲異常
         * */


        // 可能會生成的異常
        try {     // 捕獲或者聲明
            read("b.txt");
        } catch (FileNotFoundException e) {   // 使用某種捕獲,實現對異常的處理
            System.out.println(e);
            /**
             *  Throwable中的查看方法
             *  getMessage 獲取異常信息  提示給用户看的
             *  toString   獲取異常的類型和異常描述(不用)
             *  printStackTrace
             * */
            
            
            System.out.println("Throwable常用方法測試");
            System.out.println(e.getMessage()); // 文件不存在
            System.out.println(e.toString());
            e.printStackTrace();
            
            

        } finally {
            System.out.println("不管程序怎樣,這裏都會被執行");
        }

        System.out.println("over");

    }

    public static void read(String path) throws FileNotFoundException {
        if (!path.equals("a.txt")) {
            throw new FileNotFoundException("文件不存在");
        }
    }
    
}

輸出結果為:

java.io.FileNotFoundException: 文件不存在
-----Throwable常用方法測試------
文件不存在
java.io.FileNotFoundException: 文件不存在
不管程序怎樣,這裏都會被執行
over


注意事項 :try catch finally都不可以單獨使用

user avatar
0 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.