在刚接触Java的时候经常听到的一句话便是在 Java 中,Exception 是可以捕获的,Error 是不可以捕获的。但是在随着学习的深入,会发现有些观点需要重新认识下了。
Throwable 这个类是自 JDK1.0 开始就存在于 Java 的语言之中。
Throwable
首先引用一段 Oracle 官方文档上对 Throwable 的介绍Java8 Thrwoable的介绍:
The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.
Instances of two subclasses, Error and Exception, are conventionally used to indicate that exceptional situations have occurred. Typically, these instances are freshly created in the context of the exceptional situation so as to include relevant information (such as stack trace data).
太长,省略大部分了……
简单翻译下就是,Throwable 是 Error 和 Exception 的父类,并且只能是 Error 和 Exception 的实例才可以通过 throw 语句或者 Java虚拟机 抛出异常。Exception 或者 Error 是在出错的情况下新创建的,从而将出错的信息和数据包含进去。
另外在这个文档中还提到了一点就是当低层方法向高层方法抛出异常的时候,如果抛出的异常是受检查的异常,则