有这样一段代码:
public class ExceptionTest1 {
  public static void main(String[] args) {
    try {
      int a=42/0;
      int c[]={1};
      c[42]=99;
    }  catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("数组下标越界"+e); 
    } catch (ArithmeticException e) {
      System.out.println("发生了被0除"+e);
    } 
  } 
}
结果是
发生了被0除java.lang.ArithmeticException: / by zero问题:为什么第一个catch的处理结果不显示。执行正常,显然是捕获了数组下标的异常,可就是无输出。
若将int a=42/0这行删掉,第一个catch就正常了,奇怪

解决方案 »

  1.   

    你执行到int a=42/0; 时就跳到
    catch (ArithmeticException e) { 
          System.out.println("发生了被0除"+e); 
        } 
    完后程序就结束了,不会执行
     int c[]={1}; 
          c[42]=99; 
    所以结果是:
    发生了被0除java.lang.ArithmeticException: / by zero 
      

  2.   

    有异常就抛出了,不会在catch块里处理完又回到原点接着执行
    下面数据越界的代码根本就没有执行到,你在try里加些输出语句就知道了
      

  3.   

        1楼说的没错,当执行int a=42/0; 时会发生ArithmeticException异常,这时候程序就会跳转到
         catch (ArithmeticException e) { 
          System.out.println("发生了被0除"+e); 
        }
        此处执行,而try语句内的 int a=42/0; 之后的语句就不会执行了。    希望我的解答能帮助楼主!