日期:2014-05-20  浏览次数:20855 次

请教一个exception的问题
Java code
public class Ex5 {
    private static int[] ia = new int[2];
    static int x = 5;    
    public static void main(String[] args) {
        while(true) {
            try {
                ia[x] = 1;
                System.out.println(ia[x]);
                break;    
            } catch(ArrayIndexOutOfBoundsException e) {
                System.err.println(
                    "Caught ArrayIndexOutOfBoundsException");
                    e.printStackTrace();
                x--;
            } finally {
                System.out.println("Are we done yet?");        
            }
        }
        System.out.println("Now, we're done.");
    }    
}




但是myeclipse的输出是这样的

Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 5
at Ex5.main(Ex5.java:8)
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 4
at Ex5.main(Ex5.java:8)
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 3
at Ex5.main(Ex5.java:8)
Caught ArrayIndexOutOfBoundsException
Are we done yet?
Are we done yet?
Are we done yet?
Are we done yet?
1
Are we done yet?
Now, we're done.
java.lang.ArrayIndexOutOfBoundsException: 2
at Ex5.main(Ex5.java:8)

我想不明白为什么。求高人指点

------解决方案--------------------
D:\>java Ex5
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 5
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 4
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 3
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 2
at Ex5.main(Ex5.java:7)
Are we done yet?
1
Are we done yet?
Now, we're done.

D:\>java Ex5
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 5
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 4
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 3
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 2
at Ex5.main(Ex5.java:7)
Are we done yet?
1
Are we done yet?
Now, we're done.

D:\>java Ex5
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 5
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 4
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 3
at Ex5.main(Ex5.java:7)
Are we done yet?
Caught ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 2
at Ex5.main(Ex5.java:7)
Are we done yet?
1
Are we done yet?
Now, we're done.

D:\>

我在dos下没出现你的情况,可能是集成环境导致的问题
------解决方案--------------------
探讨

我的意思是说这个输出似乎是没有规律的。比如说“Are we done yet?”这一句为什么在中间连续出现?还有“Now, we're done.”是finally里面的,应该最后出现,但是为什么没有?

我猜可能是多线程吧

------解决方案--------------------