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.