【发布时间】:2014-12-03 20:23:49
【问题描述】:
如果一个操作捕获多个异常,我如何确定捕获了哪种类型的异常?
这个例子应该更有意义:
try {
int x = doSomething();
} catch (NotAnInt | ParseError e) {
if (/* thrown error is NotAnInt */) { // line 5
// printSomething
} else {
// print something else
}
}
在第 5 行,如何检查捕获了哪个异常?
我尝试了if (e.equals(NotAnInt.class)) {..},但没有成功。
注意:NotAnInt 和 ParseError 是我项目中扩展 Exception 的类。
【问题讨论】:
-
制作服务器捕获:catch (NotAnInt) { } catch (ParseError) {}