public voidtestException(){
int a[] = {1,2,3};int q = 0;
try{
for(int i=0;i
}catch(ArithmeticException h){
System.out.print("ArithmeticException\n"); //运行
}catch(Exception e){
System.out.print("Exception\n"); //不会运行。且必须放在ArithmeticException后面
/**
* 范围更大的Exception不但必须放在后面
* 并且放在后面还不会被运行(被前面的范围更小的
* 异常拦截了),那这样还有什意义呢???
*/
}finally{System.out.print("finally\n");}
}
//output
ArithmeticException
finally
*要点1:尽管ArithmeticException继承自Exception。可是当发生ArithmeticException异常
*并捕获的时候,就仅仅会捕获实际发生的这个异常。并不会由于Exception是其父类而
*运行Exception那个catch子句。
*要点2:可是假设你尝试将范围更大的Exception的catch语句放到的catch语句的前面,那么就会发生
* catch子句不可到达的错误“Unreachablecatch block for ArithmeticException.
* Itis already handled by the catch block for Exception”
*即范围更大的异常(父类)必须放在后面,假设没有继承关系,比方ClassNotFoundException,
*和ArithmeticException的catch子句之间就无所谓先后关系。