以下代码给出了所需的输出,即“发生算术异常”
public static void main(String[] args) {
try {
int []a = new int[5];
a[5] = 30/0;
} catch(ArithmeticException e) {
System.out.println("Arithmetic Exception occurs");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception occurs");
} catch(Exception e) {
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
但如果我想一次得到两个异常,修改后的代码将是
public static void main(String[] args) {
try {
int []a = new int[5];
a[10] = 30/0;
} catch(ArithmeticException e) {
System.out.println("Arithmetic Exception occurs");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBounds Exception occurs");
} catch(Exception e) {
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
但不是两者都给出,"Arithmetic Exception occurs" & "ArrayIndexOutOfBounds Exception occurs"
输出只是"Arithmetic Exception occurs"
谁能解释一下?