以前一直以为有-able的就都是接口,今天发现Throwable是一个具体的类,不是接口。Throwable有两个直接子类:Error和Exception。都说Java所有的异常可被概括为两类:checked exception class和unchecked exception class,JLS上是这样说的:The unchecked exception classed are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes.那么Throwable和Exception这两个类本身属于哪一类呢?从上面就可以知道属于checked exception classes阵营。以前一直以为一个如果catch子句声明捕捉的是一个checked exception class E,而try子句又没有可能抛出E(或它的子类)的一个实例,就会出现编译时错误,比如:
这个就不能编译通过,因为println方法不会抛出checked exception class对象,今天突然又试了一下这样的:
这段代码是可以编译通过的,当然运行也是正常的(把Exception换成Throwable也可以)。然后就又去看了下JLS11.2.3别人最后还有一小句:unless E1 is the class Exception。但它确实没有提到Throwable的情况,不过应该比较明朗一些了。
还有,以前以为ClassNotFoundException是RuntimeException,今天证实不是,不知道为啥。