我有以下简单的代码:
为什么第二个catch中调用的方法签名是
我有以下简单的代码:
class Test {
public static void main(String[] args) {
Test t = new Test();
try {
t.throwAnotherException();
} catch (AnotherException e) {
t.handleException(e);
}
try {
t.throwAnotherException();
} catch (Exception e) {
System.out.println(e.getClass().getName());
t.handleException(e);
}
}
public void throwAnotherException() throws AnotherException {
throw new AnotherException();
}
public void handleException(Exception e) {
System.out.println("Handle Exception");
}
public void handleException(AnotherException e) {
System.out.println("Handle Another Exception");
}
}
class AnotherException extends Exception {
}
为什么第二个catch中调用的方法签名是
void handleException(Exception e)
,而异常类型却是AnotherException
?