public class ArrayExceptionTest { public static void main(String[] args) { //1.数组角标越界的异常,ArrayIndexOutOfBoundsException int arr[] =new int[]{1,2,3,4,5}; // for (int i = 0; i <=arr.length ; i++) {因为不存在角标为5的情况,所以报错ArrayIndexOutOfBoundsExcetion // System.out.println(arr[i]); // } // System.out.println(arr[-2]);因为不存在角标为-2的情况,所以报错ArrayIndexOutOfBoundsExcetion //2.空指针异常,NullPointerException //情况一 // int arr1[]=new int[]{1,2,3}; // arr1=null; // System.out.println(arr1[0]);//指针指向了空的指针,所以报错NullPointerException //情况二 // int arr2[][]=new int[4][];//此时外层数组中的元素默认值是整型的null,内层数组都还没有创建(有及格元素都不知道所以指向哪里就更不知道了。)是空的。 // System.out.println(arr2[0][0]);//当此时指向外层的数组时,输出的是整型的null,当指向指向内层时候,没有能指的地方。 }
Array的异常处理
最新推荐文章于 2023-05-13 16:06:40 发布