Java中异常的分类

来源:百度知道 编辑:UC知道 时间:2024/06/17 21:27:03
例如根据什么来分类,及他们的区别

程序出了那种异常JVM就会抛出相应的异常
比如代码:ArrayIndexOutOfBoundsException
public void demo1(){
try{
int a = 10/0;
}catch(ArithmeticException ae){
System.out.println(“算术运算异常:”+ae.getMessage());
}catch(Exception e){
System.out.println(“其他异常”+e.getMessage());
}
}

public void demo2(){
String strList[] = {"a","b","c"};
try{
String str = strList[4].toString();
}catch(ArrayIndexOutOfBoundsException ae){
System.out.println(“数组下标越界:”+ae.getMessage());
}catch(Exception e){
System.out.println(“其他异常”+e.getMessage());
}
}
总之Exception是所有异常的父类.如果你出现的异常被他的子类捕捉了,他就不会再捕捉比如demo2()方法如果是出现了ArrayIndexOutOfBoundsException
Exception就不会捕捉了!
那么为什么要捕捉多次呢?因为ArrayIndexOutOfBoundsException只是数组下标越界的异常,所以它比Exception更的仔细,更能说明异常的原因!
如果不是出现ArrayIndexOutOfBoundsExcep