/*
对多异常的处置
1.性命异常时,建议声明更加具体的异常,这样处置可以更具体
2.对方声明几个异常,就应有几个catch块。
如果多个catch块中的异常涌现继承关系,父类异常catch块放在最下面。
不要定义过剩的catch块。
3.建议在进行catch处置时,catch中必定要定义具体的处置方法,不要
简略定义一句e.printStackTrace(),也不要简略的输出一条语句。
一般在catch块中写存储日志代码。将异常日志写到硬盘中。
*/
class Demo{
int div(int a,int b)throws AritchmeticException,ArrayIndexOutOfBoundsException//在功效上通过throws声明了该功效可能会涌现问题
{
int[] arr = new int[a];
System.out.println(arr[4]);
return a/b;//jvm在这检测到异常,new AritchmeticException()
}
}
/*class Test{
public static void main(String[] args){
Demo d = new Demo();
int x = d.div(4,0);
System.out.println("x="+x);
System.out.println("over");
}
}*/
class Test{
public static void main(String[] args){
Demo d = new Demo();
try{
int x = d.div(4,0);//new AritchmeticException()
System.out.println("x="+x);
}
catch(AritchmeticException e){//Exception e = new AritchmeticException()
System.out.println("分母为0了");
System.out.println(e.getMessage());//信息异常
System.out.println(e.toString());//异常名称:异常信息
e.printStackTrace();//异常名称,异常信息,异常涌现的地位
//jvm默许的异常处置机制,就是在调用printStackTrace办法
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e.toString());
System.out.println("角标出界");
}
System.out.println("over");
}
}/*
对多异常的处置
1.性命异常时,建议声明更加具体的异常,这样处置可以更具体
2.对方声明几个异