什麼是 Java 中的非受檢異常?


非受檢異常是指在執行時發生的異常。這些異常也稱為執行時異常。其中包括程式設計錯誤,如邏輯錯誤或不當使用 API。在編譯時會忽略執行時異常。 

如果你在程式中聲明瞭一個大小為 5 的陣列,並嘗試呼叫陣列的第 6 個元素,則會出現 ArrayIndexOutOfBoundsException 異常。

示例

演示

public class Unchecked_Demo {
   public static void main(String args[]) {
      int num[] = {1, 2, 3, 4};
      System.out.println(num[5]);
   }
}

輸出

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
   at Exceptions.Unchecked_Demo.main(Unchecked_Demo.java:8)

更新時間:2020-6-16

2 千+ 瀏覽量

開啟你的職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.