【发布时间】:2014-09-02 14:01:36
【问题描述】:
在 oracle 官方网站上写 (http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html#rethrow)
详细地说,在 Java SE 7 及更高版本中,当您声明一个或多个 catch 子句中的异常类型,并重新抛出处理的异常 通过这个 catch 块,编译器验证重新抛出的异常的类型满足以下条件:
-try 块可以抛出它。
-没有其他前面的 catch 块可以处理它。
-它是 catch 子句的异常参数之一的子类型或超类型。
请专注于第二点 (There are no other preceding catch blocks that can handle it.)
研究以下代码:
static private void foo() throws FileNotFoundException {
try {
throw new FileNotFoundException();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
throw e;
}
}
这段代码编译得很好。根据我在阅读上述文章的引用后的看法,我希望看到编译器会验证它并且我会得到编译器错误。
第二点我理解错了吗?
【问题讨论】:
标签: java exception-handling try-catch java-7 rethrow