程序出了那种异常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更的仔细,更能说明异常的原因!
如果不是出现ArrayIndexOutOfBoundsException则Exception就会来捕捉
题目太大了,不好回答
看一下JDK关于异常的类图就明白了
Throwable下面分为error,exception等
具体的异常,你只要看异常的类名就明白个大概了
硬要说的话分两大类
普通异常
和
运行时异常
区别在于
普通异常必须在编译的时候进行异常捕获要不然通不过编译
运行时异常不是必须捕获的