这可能看起来像一个编程101问题,我以为我知道答案,但现在发现自己需要再次检查。在下面这段代码中,第一个捕获块中抛出的异常会被下面的通用异常捕获块捕获吗?
try {
// Do something
} catch(IOException e) {
throw new ApplicationException("Problem connecting to server");
} catch(Exception e) {
// Will the ApplicationException be caught here?
}
我一直认为答案是否定的,但现在我有一些奇怪的行为可能是由这引起的。大多数语言的答案可能都是一样的,但我用的是Java。
Java语言规范在14.19.1节中说:
If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
If the run-time type of V is assignable to the Parameter of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. If that block completes normally, then the try statement completes normally; if that block completes abruptly for any reason, then the try statement completes abruptly for the same reason.
参考:
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#24134
换句话说,第一个可以处理异常的封闭捕获可以,如果从该捕获抛出异常,则该异常不在原始try的任何其他捕获的范围内,因此它们不会尝试处理它。
需要知道的一件相关且令人困惑的事情是,在try-[catch]-finally结构中,finally块可能会抛出异常,如果是这样,try或catch块抛出的任何异常都会丢失。第一次看到的时候可能会感到困惑。