请教一个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下没出现你的情况,可能是集成环境导致的问题
[解决办法]
------解决方案--------------------
另外3楼用控制台输出正常,
说明是IDE在同时处理System.out和System.err输出的时候使用了未加限制的多线程(我用的Netbeans),
这是典型的 race condition。
