【发布时间】:2015-01-12 08:59:39
【问题描述】:
在下面的程序中,当我们尝试获取 main exception 表示异常时,它会导致程序的第一个异常,然后它会给出类似这样的错误:cannot find symbol : e.getCause(); 但在删除此语句 System.out.println("Main Cause : " + e.getCause()); 后,我们成功获得了第一例外。
class chain_demo {
static void demo() {
//create an exception
ArithmeticException e = new ArithmeticException("top Module");
//cuase an exception
e.initCause(new NullPointerException("Cause"));
//throw exception
throw e;
}
public static void main(String args[]) {
try {
demo();
}
catch(ArithmeticException e) {
//display top_level exception
System.out.println("Cautch : " + e);
}
//display cause exception
//getting error here
System.out.println("Main Cause : " + e.getCause());
}
}
那么,我怎样才能得到原因异常。
【问题讨论】:
标签: java exception-handling chained