Throwable、Error、Exception、RuntimeException的说明

1.Throwable类

在Java中,根据源代码可知,Error和Exception均继承自Throwable类,关于Throwable类,Java里的介绍是:

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.

在这里第一次引出了一个概念——检查异常(checked exceptions),它的定义是“Throwable和Throwable的任何子类(不是RuntimeException或Error的子类)都被视为检查异常”,因此我们知道了RuntimeException和Error都是非检查异常(unchecked exceptions)。

2.Error类

Java关于Error类的介绍为:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a “normal” condition, is also a subclass of Error because most applications should not try to catch it.

由上可知Error不需要try-catch,并且线程死亡这个看似正常的情况,也不需要被捕获,有趣是关于ThreadDaed类的介绍:

An instance of ThreadDeath is thrown in the victim thread when the (deprecated) Thread.stop() method is invoked.

An application should catch instances of this class only if it must clean up after being terminated asynchronously. If ThreadDeath is caught by a method, it is important that it be rethrown so that the thread actually dies.

The top-level error handler does not print out a message if ThreadDeath is never caught.

The class ThreadDeath is specifically a subclass of Error rather than Exception, even though it is a “normal occurrence”, because many applications catch all occurrences of Exception and then discard the exception.

同样,它也异常简单:

public class ThreadDeath extends Error {
    private static final long serialVersionUID = -4417128565033088268L;
}

大概意思就是ThreadDead只有在Thread.stop()方法执行时才抛出,当它被抛出时,线程就会死亡。最关键的是最后一段,不把ThreadDead定义为Exception是因为许多应用程序都会捕获Exception的所有事件,然后抛出异常。这个类被设计出来就是为了停止线程,但因为方法过于暴力,释放当前线程的所有锁,并且将线程停止。这种作法会导致该线程所操作的数据不一致,比如:当前线程为了更新某个数据,而获取了某个锁,然后在更新中途,调用stop时,然后另一个线程访问该对象时,就会拿到一个数据不一致的对象,导致错误。

3.Exception类

Java关于Exception类的介绍为:

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor’s throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

我们知道了,Exception类是希望被捕获的,Exception类和所有子类(不包括RuntimeException类及其子类)都是检查异常(checked exceptions),它们需要在方法或构造方法的throws子句中声明,可以传递到外部。

4.RuntimeException类

Java关于RuntimeException类的介绍为:

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor’s throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

由上可知,RuntimeException是可以在JVM正常运行期间抛出,而其他Exception及其子类(不包括RuntimeException类及其子类)是在编译时抛出。


以上便是根据源代码分析这4个类,大家如果有什么疑问,欢迎再下方留言,博主将尽力解答。

猜你喜欢

转载自blog.csdn.net/death05/article/details/78540180