【发布时间】:2017-05-09 07:38:27
【问题描述】:
我自己写了两个自定义异常,一个是选中的,另一个是未选中的 当我执行我的代码时只显示检查异常为什么我无法获得未经检查的异常输出??
class Test {
public static void main(String args[]) throws CheckedException {
int i=0;
int j=0;
if(i==0){
throw new CheckedException("Got Checked Exception");
}
if(j==0){
throw new UncheckedException("Got Unchecked Exception");
}
}
}
class CheckedException extends Exception{
CheckedException(String s){
super(s);
}
}
class UncheckedException extends RuntimeException{
UncheckedException(String s){
super(s);
}
}
上述程序的输出是:Got Checked Exception ,但我期待两个输出 Got Checked Exception && Got Unchecked Exception。我在这里犯了什么错误?我该如何克服这个问题?
【问题讨论】:
-
因为 UncheckedException 不可访问。将
int i=0更改为i=1,您会看到UncheckedException -
你看不到这两个例外。如果第一个被抛出,该方法将离开。
-
好的,现在我先写了 UncheckedException 然后 CheckedException 现在输出显示 UncheckedException ,但是我想要两个 Exception ,有没有办法同时获得两个异常输出??
-
不,没有。如果您使用调试器,您就会明白为什么。抛出第二个异常的代码没有被执行
-
不客气
标签: exception exception-handling checked-exceptions unchecked-exception