class Demo
{
public void method(int[] arr,int index)
{
if(arr == null)
{
throw new NullPointerException("数组的引用不能为空!");
}
if(index >= arr.length)
{
throw new ArrayIndexOutOfBoundsException("数组的角标越界了,兄弟。"+index);
}
if(index < 0)
{
throw new ArrayIndexOutOfBoundsException("数组的角标不能为负数,兄弟"+index);
}
System.out.println(arr[index]);
}
}
public class ExceptionDemo2 {
public static void main(String[] args) {
int[] arr = new int [3];
new Demo().method(arr,-3);
}
}
异常----异常对象的抛出
最新推荐文章于 2024-01-12 11:03:17 发布