Throwable、Error、Exception、RuntimeExceptionの説明

4234 ワード

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クラスとそのサブクラスを含まない)は、メソッドまたは構築メソッドのthrows句で宣言し、外部に渡す必要があるチェック例外(checked exceptions)です.
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つのクラスを分析しました.もし何か質問があれば、下のメッセージを歓迎します.ブロガーは全力を尽くして答えます.