为什么在Java中我们可以捕获一个Exception
即使它没有被抛出,但我们无法捕获它的子类(除了"unchecked" RuntimeException
和它的子类).示例代码:
class Test {
public static void main(String[] args) {
try {
// do nothing
} catch (Exception e) {
// OK
}
try {
// do nothing
} catch (IOException e) {
// COMPILER ERROR: Unreachable catch block for IOException.
//This exception is never thrown from the try statement body
}
}
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?