为什么在 Java 中Exception
即使它没有被抛出,我们也能捕捉到它,但我们不能捕捉到它的子类(“unchecked” RuntimeException
s 和它的子类除外)。示例代码:
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
}
}
}
有任何想法吗?