异常

父类 throwable
子类 Error 和 Exception
Error 不可恢复,不需要捕获
Exception 分为两类

  1. checkException,需要手动try catch ,如IOException

2.unCheckedException(RuntimeException)。如 IllegalArgementException,IndexOutofBoundException,NullPointerException

3.同时抛出多层异常,小范围的异常需放在前面,否则可能永远访问不到

4.测试

 try{
  int i  = 10/0; //①
  System.out.println("end");  //②
} catch(IOException e){
  System.out.println("catch"); //③
  throw new IllegalArgumentException(e); //④
}finally {
     System.out.println("finally"); //⑤
     throw new NullPointerException(e); // ⑥
}
  System.out.println("bye bye");  //⑦

执行顺序:① → ③ → ⑤ → ⑥
如果没有 ④ 和 ⑥,执行顺序为: ① → ③ → ⑤ → ⑦

你可能感兴趣的:(异常)