public class MyException extends Exception{
}
public class MyException2 extends MyException{
}
public class ExceptionTest {
public static void main(String[] args) {
try {
throw new MyException();
} catch(MyException2 m){
System.out.println("MyException2 runing");
}
catch (MyException m) {
System.out.println("MyException runing");
}
catch (Exception e) {
System.out.println("Exception runing");
}
finally{
System.out.println("finally runing");
}
}
}
当存在多个catch块的时候,由于Exception是所有异常类的老爹,所以它必须放在最后,而且是异常类的范围自上向下的过程catch块中异常类逐渐加大,顺序不能颠倒,而且一旦某个catch块捕获了异常,后续的catch块将不会执行,直接前往finally执行(前提存在finally)