各位看官好,下面同大家一起看一个关于java异常丢失的问题。
在使用try-catch-finally进行java异常捕获时,存在以下问题:
如果finally子句中存在异常,同时catch中未正确捕获到try中的异常,则try中的异常将会被finally中的异常所覆盖,从而导致异常的丢失现象。
此时结果如下:
[color=red]Exception in thread "main"[/color] [color=darkblue][u]java.lang.ArrayIndexOutOfBoundsException[/u][/color][color=red]: 100[/color]
[color=red]at c09.TestException.main([/color][u][color=darkblue]TestException.java:13[/color][/u][color=red])[/color]
可以清楚看出,try中的算术异常被丢失了。就酱~ :arrow:
在使用try-catch-finally进行java异常捕获时,存在以下问题:
如果finally子句中存在异常,同时catch中未正确捕获到try中的异常,则try中的异常将会被finally中的异常所覆盖,从而导致异常的丢失现象。
public class TestException {
public static void main(String[] args) {
try {
//try中存在算术异常ArithmeticException
int i=1/0;
System.out.println(i);
} catch (NullPointerException e) {
e.printStackTrace();
}finally{
int [] i=new int[10];
//finally中存在数组越界异常ArrayIndexOutOfBoundsException
System.out.println(i[100]);
}
}
}
此时结果如下:
[color=red]Exception in thread "main"[/color] [color=darkblue][u]java.lang.ArrayIndexOutOfBoundsException[/u][/color][color=red]: 100[/color]
[color=red]at c09.TestException.main([/color][u][color=darkblue]TestException.java:13[/color][/u][color=red])[/color]
可以清楚看出,try中的算术异常被丢失了。就酱~ :arrow: