首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请问一个exception的有关问题

2012-08-21 
请教一个exception的问题Java codepublic class Ex5 {private static int[] ia new int[2]static int x

请教一个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里面的,应该最后出现,但是为什么没有?

我猜可能是多线程吧

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


另外3楼用控制台输出正常,
说明是IDE在同时处理System.out和System.err输出的时候使用了未加限制的多线程(我用的Netbeans),
这是典型的 race condition。

热点排行
Bad Request.