可是
catch(ArrayIndexOutOfBoundsException e){ //捕获数组下标越界异常
System.out.println("Array index oob:"+e);
}
并没有报错呀
能说说数组下标的越界问题吗

解决方案 »

  1.   

    public class welcome
    {
     public static void main(String[]grav)
     {
        try{
        int c[] = {1};
        System.out.println(c[4]);
        }catch(ArrayIndexOutOfBoundsException e){ //捕获数组下标越界异常
          System.out.println("Array index oob:"+e);
         } }
    }
    怎么不会,你运行一下这个试试
      

  2.   

    感谢大侠赐教
    但请问为什么不调用
    System.out.println(c[4]);
    就不出现异常错误
    请指教
      

  3.   

    感谢大侠赐教
    请问下面的类为什么不调用class ExceptionDemo2{
    public static void main(String args[]){
    try{
    int a=args.length;
    System.out.println("a="+a);
    int b=42/a;
    int c[]={1};
    c[4]=99;
    }
    catch(ArithmeticException e){
    System.out.println("Divide by 0:"+e);
    }
    catch(ArrayIndexOutOfBoundsException e){
    System.out.println("Array index ob:"+e);
    }
    System.out.println("After try/catch blocks");
    }
    }
      

  4.   

    这是因为程序运行到int b=42/a;
    这后就出错,转到了catch(ArithmeticException e){
    System.out.println("Divide by 0:"+e);
    }
    根本就没运行int c[]={1};c[4]=99;
    所以没有打出Array index ob:"+e
      

  5.   

    Array index ob:java.lang.ArrayIndexOutOfBoundsExceptionAfter try/catch blocks上面的程序不是也会出index out of bound的错误吗?不是很正常吗?
      

  6.   

    我刚才在 vj++下试了一下,不论是c[4]=99;还是System.out.println(c[4]);
    都会触发异常.上面这个类会触发 ArithmeticException 异常(如果你不输入命令行参数)
    如果输入命令行参数会触发ArrayIndexOutOfBoundsException异常当捕获多个异常时,只能捕获到一个异常,所以在写捕获多个异常的语句时,要注意异常捕获顺序。
      

  7.   

    不是啊,我直接用java命令做的,程序到了int b=42/a;就发生异常抛出了,后面的程序就不运行了,当然没有数据越界的异常抛出,但上面的System.out.println("After try/catch blocks");是在try外面的,所以会打出