有这样一段代码:
public class ExceptionTest1 {
public static void main(String[] args) {
try {
int a=42/0;
int c[]={1};
c[42]=99;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界"+e);
} catch (ArithmeticException e) {
System.out.println("发生了被0除"+e);
}
}
}
结果是
发生了被0除java.lang.ArithmeticException: / by zero问题:为什么第一个catch的处理结果不显示。执行正常,显然是捕获了数组下标的异常,可就是无输出。
若将int a=42/0这行删掉,第一个catch就正常了,奇怪
public class ExceptionTest1 {
public static void main(String[] args) {
try {
int a=42/0;
int c[]={1};
c[42]=99;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界"+e);
} catch (ArithmeticException e) {
System.out.println("发生了被0除"+e);
}
}
}
结果是
发生了被0除java.lang.ArithmeticException: / by zero问题:为什么第一个catch的处理结果不显示。执行正常,显然是捕获了数组下标的异常,可就是无输出。
若将int a=42/0这行删掉,第一个catch就正常了,奇怪
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货