异常

父类 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");  //⑦

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。