Java 中的非受检异常是什么?

java 8object oriented programmingprogramming

非受检异常是指在执行过程中发生的异常。这些异常也称为运行时异常。这些异常包括编程错误,例如逻辑错误或 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)

相关文章