哪位高手帮我分析一下这个java程序的执行结果

来源:百度知道 编辑:UC知道 时间:2024/05/22 03:17:28
程序是这样的:
public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println(
"ArrayIndexOutOfBoundsException" +
"/内层try-catch");
}

throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(
"ArrayIndexOutOfBoundsException" +
"/外层try-catch");
}
}
}
为什么运行结果只有”ArrayIndexOutOfBoundsException/外层try-catch”这一句,而没有得到以下这一句”发生ArithmeticException”?

按程序顺序执行,在执行到
try {
throw new ArrayIndexOutOfBoundsException();
}
时捕获ArrayIndexOutOfBoundsException(); 异常,不再往下执行,而是向外抛出该异常给外层,外层catch到这个异常,并处理该异常。
也就是说,
下面这段程序根本没运行。
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}

我怀疑你是复制的吗~?

ArrayIndexOutOfBoundsException/外层try-catch

内层捕获之后,再抛给外层~~~
下层捕获了,上层当作没有异常,正常执行了

你的程序很乱,我也懒得看,现实中还没有遇到你这样写法的呢。。。我给你个建议吧,在eclipse中用设断点追踪观察的办法,单步执行,你就能直观看到它的执行顺序!