怎么能编写个测试数组越界的类呢?并用异常检错.我写的这个哪错了呢???
package qq;public class sguZhu {
    public sguZhu() {
    }    int[] arry = {1, 2, 3};    public static void main(String[] args) {
        try {        } catch (Exception ex) {        } finally {
            System.out.println("error");        }        System.out.println(arry[6]);
    }
}

解决方案 »

  1.   

    arry[]你定义的3个  怎么写了arry[6]呢
      

  2.   

    public class sguZhu {
        public sguZhu() {
        }    int[] arry = {1, 2, 3};    public static void main(String[] args) {
            try {
                System.out.println(arry[6]);
     
            } catch (Exception ex) {
                 ex.getMessage();
            } finally {
                System.out.println("end");        }        
        }
    }
      

  3.   

    package qq;public class sguZhu {
        public sguZhu() {
        }    int[] arry = {1, 2, 3};    public static void main(String[] args) {
            try {
               System.out.println(arry[6]);        } catch (ArrayIndexOutOfBoundsException e) {
               System.out.println("数组越界");
            } finally {        }        
        }
    }
      

  4.   

    public class sguZhu {
        public sguZhu() {
        }    int[] arry = {1, 2, 3};    public static void main(String[] args) {
            try {
                System.out.println(arry[6]);
     
            } catch (Exception ex) {
                 ex.getMessage();
            } finally {
                System.out.println("end");        }        
        }
    }
      

  5.   

    package qq;public class sguZhu {
        public sguZhu() {
        }    int[] arry = {1, 2, 3};    public static void main(String[] args) {
            try {
                System.out.println(arry[6]);
            } catch (Exception ex) {
                ex.getMessage();
            } finally {
                System.out.println("error");        }
        }
    }
      

  6.   

    System.out.println(arry[6]);
    这是你自己定义的异常,要捕获也应该放到try{}cathc{}块里,try捕获,catch解决。。
      

  7.   

    大家只注意到捕获异常了,其实楼主的语法还有个错误,main里面没有创建类对象就用它的array[]肯定是不行的啊
    public class Test {
        
        int[] arry = {1, 2, 3};    public static void main(String[] args) {
    Test t1 = new Test();
            try {
                System.out.println(t1.arry[6]);
     
            } catch (Exception ex) {
                 ex.getMessage();
            } finally {
                System.out.println("end");        }    
        }
    }
      

  8.   

    没错,
    int[] arry = {1, 2, 3};要写成static int[] arry = {1, 2, 3};也行。
      

  9.   

    小第刚接触java 自学的,不过真的要谢谢你们的帮忙.还希望以后多多支持.不胜感激
      

  10.   

    应该这么说:
    int array[]={1,2,3};int array[3];
    array[3]={1,2,3};这就是常见的定义数组并赋初值了..