throw关键字
public class Demo03Throw {
public static void main(String[] args) {
// int[] arr = null;//Exception in thread "main" java.lang.NullPointerException: 传递的数组为null
int[] arr = new int[3];//0
int e = getElement(arr,3);//若书3则会ArrayIndexOutOfBoundsException
System.out.println`在这里插入代码片`(e);
}
public static int getElement(int[] arr,int index){
if(arr == null){
//Exception in thread "main" java.lang.NullPointerException: 传递的数组为null
throw new NullPointerException("传递的数组为null");
}
if(index < 0 || index > arr.length - 1){
throw new ArrayIndexOutOfBoundsException();
}
int ele = arr[index];
return ele;
}
}