【发布时间】:2015-11-27 11:56:12
【问题描述】:
我真的很想使用 Java-1.7 的功能。此功能之一是“多捕获”。目前我有以下代码
try {
int Id = Integer.parseInt(idstr);
TypeInfo tempTypeInfo = getTypeInfo(String.valueOf(Id));
updateTotalCount(tempTypeInfo);
} catch (NumberFormatException numExcp) {
numExcp.printStackTrace();
} catch (Exception exception) {
exception.printStackTrace();
}
我想从上面的代码中删除两个 catch 块,而是使用如下的单个 catch:
try {
int Id = Integer.parseInt(idstr);
TypeInfo tempTypeInfo = getTypeInfo(String.valueOf(Id));
updateTotalCount(tempTypeInfo);
} catch (Exception | NumberFormatException ex) { // --> compile time error
ex.printStackTrace();
}
但是上面的代码给出了编译时错误:
"NumberFormatException" 已被替代方法捕获 例外。
我理解上面的编译时错误,但是我的第一个代码块的替换是什么。
【问题讨论】:
标签: java exception try-catch numberformatexception multi-catch