有这样一段代码:
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就正常了,奇怪
c[42]=99;
这两行,你哪行写在前面,捕获的就会是它对应的异常。
int c[]={1};
c[42]=99;
明明是两个异常,不能同时出现啊.
有一个就已经停了.
这句执行完已经发生异常,程序进入了catch块 int c[]={1};
c[42]=99;
这两句根本就没执行这样写两个异常就都被捕获了
public class ExceptionTest1 {
public static void main(String[] args) {
try {
int a=42/0;
} catch (ArithmeticException e) {
System.out.println("发生了被0除"+e);
}
try {
int c[]={1};
c[42]=99;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界"+e);
}
}
}
这句执行完已经发生异常,程序进入了catch块 int c[]={1};
c[42]=99;
这两句根本就没执行
import org.apache.log4j.Logger;public class Test extends TestCase { private Logger log = Logger.getLogger(Test.class); public void test() {
log.info("test1");
try {
int a=42/0;
}catch (ArithmeticException e) {
System.out.println("发生了被0除"+e);
try{
int c[]={1};
c[42]=99;
}catch (ArrayIndexOutOfBoundsException e2) {
System.out.println("数组下标越界"+e2);
}
}
}
}
test1
发生了被0除java.lang.ArithmeticException: / by zero
数组下标越界java.lang.ArrayIndexOutOfBoundsException: 42