当前位置: 代码迷 >> J2SE >> 错误捕获不到了
  详细解决方案

错误捕获不到了

热度:82   发布时间:2016-04-24 12:46:34.0
异常捕获不到了、

public class CatchWho2 {

public static void main(String[] args) {
try{
try{
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e){
System.out.println("ArrayIndexOutOfBoundsException"+
"/内层try-catch");
}
throw new ArithmeticException("yichang");
}
catch(ArithmeticException e){
System.out.println("发生:ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBoundsException/外层try-catch");
}
}

}
很想知道这个程序中,throw new ArithmeticException这句抛出的异常怎么捕获不到、catch里没有执行这个异常 


------解决方案--------------------
你执行到throw new ArrayIndexOutOfBoundsException(); 时,由于抛出异常,程序跳转到
catch(ArrayIndexOutOfBoundsException e){ 
System.out.println("ArrayIndexOutOfBoundsException/外层try-catch"); 

继续执行,你的throw new ArithmeticException("yichang"); 是在内层的try里面的,由于你执行到throw new ArrayIndexOutOfBoundsException(); 时已经跳转出内层的try了,不会再返回,直接输出ArrayIndexOutOfBoundsException/外层try-catch;

------解决方案--------------------
2楼的回答很正确,意见一致!
------解决方案--------------------
明白了··谢谢,学习了·
  相关解决方案