/*
异常的分类:
1.编译时被检测异常:只要是Exception和其他子类都是,除了特殊子类RuntimeException体系这种问题一旦出现,希望在编译时就进行检测,让这种问题有对应的处理方式
这样的问题都可以针对性出来
2.编译时不检测异常(运行时异常):就是Exception中的RuntimeException和其子类
这种问题的发生,无法让功能继续,运算无法进行,更多的是因为调用者原因导致的或者引发了
内部状态改变导致的,那么这种问题一般不处理,直接编译通过,在运行时,让调用者调用时的程序
强制制止,让调用者进行修订
所以自定义异常时,要么继承Excception。要么继承RuntimeException
throws与throw区别
1.throws使用在函数上 throw使用在函数内
2.throws抛出异常类,可以抛出多个,用逗号隔开
throw抛出的是异常对象
*/
class FuShuIndexException extends Exception
{
FuShuIndexException()
{}
FuShuIndexException(String msg)
{
super(msg);
}
}
class Demo
{
public int method(int[] arr,int index) throws FuShuIndexException //抛出声明
{
if(arr==null)
throw new NullPointerException("数组的引用不能为空! ");
if(index>=arr.length)
{
throw new ArrayIndexOutOfBoundsException("数组的角标越界啦,哥们,你是不是疯"+index);
}
if(index<0)
{
throw new FuShuIndexException("角标变成负数啦!! ");//预先出来方式
}
return arr[index];
}
}
class ExceptionDemo3
{
public static void main(String[] args) throws FuShuIndexException//抛出声明
{
int[] arr = new int[3];
Demo d = new Demo();
int num = d.method(arr,-30);
System.out.println("num="+num);
System.out.println("over");
}
}