java 基本语法(十三) 数组(六)数组的常见异常

1.数组角标越界异常:ArrayIndexOutOfBoundsException

        int[] arr = new int[]{1,2,3,4,5};
        
//        for(int i = 0;i <= arr.length;i++){
//            System.out.println(arr[i]);
//        }
        
//        System.out.println(arr[-2]);
        
//        System.out.println("hello");
2.空指针异常:NullPointerException
    //情况一:
//        int[] arr1 = new int[]{1,2,3};
//        arr1 = null;
//        System.out.println(arr1[0]);
        
        //情况二:
//        int[][] arr2 = new int[4][];
//        System.out.println(arr2[0][0]);
        
        //情况:
        String[] arr3 = new String[]{"AA","BB","CC"};
        arr3[0] = null;
        System.out.println(arr3[0].toString());

小知识:一旦程序出现异常,未处理时,就终止执行。
原文地址:https://www.cnblogs.com/hzcya1995/p/13308902.html