Java基础--异常--对多异常的处理
对多异常的处理
1、声明异常时,建议声明更为具体的异常,这样处理的可以更加具有针对性
2、对方声明几个异常,就应该对应有几个catch块,不要定义多余的catch块
如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。
注意:
/* 建立在进行catch处理时,catch中一定要定义具体处理方式。
不要简单定义一句 e.printStackTrace(),----因为用户看不懂
也不要简单地书写一条输出语句 */
真发生问题,在catch里面建立、记录问题到日志文件中,供维护人员查看。
--------------------------
ExceptionTest.java
public class ExceptionTest {
public static void main(String[] args){
DivDemo dd =new DivDemo();
int resultD;
try {
resultD = dd.div(10, 0);
System.out.println("resultD' value is :"+resultD);
} catch (ArithmeticException e) {
e.printStackTrace();
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
System.out.println("over!");
}
}
class DivDemo{
public int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException {
int [] arr = new int[5];
System.out.println(arr[a]);
return a/b;
}
}
:console:



浙公网安备 33010602011771号