package test;
public class Testdate {
public static void main(String[] args) {
try{
int [] a = new int[3];
a[0] = 9;
a[1] =10;
a[2] = 12;
a[3] = 13;
for(int i = 0;i<a.length;i++){
System.out.println(a[i]);
if(i>3) throw new myException("下标越界");
}

}
catch(myException ee){
System.out.println("ljklkj");
System.out.println(ee.toString());
}
finally{
System.out.println("数组练习");
}
}
}
//
class myException extends Exception{
public myException(){
super();
}
public myException(String message){
super(message);
}

}

解决方案 »

  1.   

    int [] a = new int[3];
    a[0] = 9;
    a[1] =10;
    a[2] = 12;
    a[3] = 13; 
    这样初始化的是3个对象吧,就是a[0],a[1],a[2],访问a[3]就数组越界了。
      

  2.   

    int [] a = new int[3]; 
    a[0] = 9; 
    a[1] =10; 
    a[2] = 12; 
    a[3] = 13; 你申明一个长度为3的数组那你的下标就只有2(0,1,2)数组长度和下标是不同的
      

  3.   

    把int [] a = new int[3];这句改成
    int [] a = new int[4];
    这样才不会越界。
      

  4.   

    应该在a[3] = 13; 的那一行抛出数组越界的异常,根本走不到你判断的地方。当然Catch不到你的异常了。老大,看看Java抛出的调用Stack应该很明确的啊。
      

  5.   

    或者象上面说的,定义int [] a = new int[4]; 这样不会出异常。或者去catch ArrayIndexOutOfBoundsException,不要用你自己定义的Exception或者你定义的Exception 去extends ArrayIndexOutOfBoundsException
      

  6.   

    if(i>3) 改成if(i>=3)
    楼主是这意思么? 
      

  7.   

    package test;
    public class Testdate { 
    public static void main(String[] args) { 
    try{ 
    int [] a = new int[4]; 
    a[0] = 9; 
    a[1] =10; 
    a[2] = 12; 
    a[3] = 13; 
    for(int i = 0;i <=a.length;i++){ 
    if(i>3) throw new myException("下标越界"); 
    System.out.println(a[i]); 
    } } 
    catch(myException ee){ 
    System.out.println("ljklkj"); 
    System.out.println(ee.toString()); 

    finally{ 
    System.out.println("数组练习"); 



    // 
    class myException extends Exception{ 
    public myException(){ 
    super(); 

    public myException(String message){ 
    super(message); 
    } } 
    这样估计可以看到你的异常处理.
      

  8.   

    int [] a = new int[3]; 就是表示数组的大小为3,JAVA中的数组下标是从0开始的,所以你的a[3]其实是表示的数组中的第4个元素。故会出现数组越界错误哈。
      

  9.   


    package test;
    public class Testdate { 
    public static void main(String[] args) { 
    try{ 
    int [] a = new int[3]; 
    a[0] = 9; 
    a[1] =10; 
    a[2] = 12; 
    a[3] = 13; 
    for(int i = 0;i <a.length;i++){ 
    System.out.println(a[i]); 
    if(i>3) throw new ArrayIndexOutOfBoundsException("下标越界"); 
    }  }
    catch(ArrayIndexOutOfBoundsException e){ 
    System.out.println("ljklkj"); 
    System.out.println(e.toString()); 

    finally{ 
    System.out.println("数组练习"); 


    } 输出:
    ljklkj
    java.lang.ArrayIndexOutOfBoundsException: 3
    数组练习
      

  10.   

    int [] a = new int[3]; //这有错
    至于为什么你所期望的异常没有执行是因为
    程序在还没有到你写的异常的时候(在a[3] = 13就终止了),
    系统就为你报了错误,他为你抛出数组下标越界的异常