方法声明抛出Exception,实际抛出为子类Exception,具体catch时会被子类Exception块捕获处理。
public class TestException extends Exception{
public void throwException() throws Exception {
TestException exception = new TestException();
throw exception;
}
public static void main(String[] args) {
TestException t = new TestException();
try {
t.throwException();
} catch (TestException e) {
System.out.println("a"); //会走这里
} catch (Exception e) {
System.out.println("b");
}
}
}