Why in Java can we catch an Exception
even if it is not thrown, but we can't catch it's subclass (except for "unchecked" RuntimeException
s and it subclasses). Example code:
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
}
}
}
Any ideas?