throw new ArrayIndexOutOfBoundsException("数组角标越界:"+index);
}
return arr[index];
}
}
public class ExceptionDemo {
public static void main(String[] args) {
int[] arr = new int[3];
Demo.method(arr, 30);
}
}
复制代码
运行结果:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 数组角标越界:30
at cn.fuxi.exception.Demo.method(ExceptionDemo.java:38)
at cn.fuxi.exception.ExceptionDemo.main(ExceptionDemo.java:47)
public static int method(int[] arr,int index) throws FuShuIndexException {
if(index<0){
throw new FuShuIndexException("数组的角标是负数");
}
return arr[index];
}
}
public class ExceptionDemo2 {
public static void main(String[] args) throws FuShuIndexException {
int[] arr = new int[5];
Demo2.method(arr, -6);
}
}
复制代码
运行结果:
Exception in thread "main" cn.fuxi.exception.FuShuIndexException: 数组的角标是负数
at cn.fuxi.exception.Demo2.method(ExceptionDemo2.java:37)
at cn.fuxi.exception.ExceptionDemo2.main(ExceptionDemo2.java:46)
运行结果:
Exception in thread "main" cn.fuxi.exception.FuShuIndexException2: 数组的角标是负数啦!
at cn.fuxi.exception.Demo3.method(ExceptionDemo3.java:24)
at cn.fuxi.exception.ExceptionDemo3.main(ExceptionDemo3.java:33)