java - 为什么ThreadDeath类不称为ThreadDeathError呢?

标签 java error-handling throwable

Java中几乎所有的Error子类在名称中都包含“-Error”后缀,但不包含ThreadDeath。为什么是这样?这使得很难记住它是Error还是Exception的子类。

最佳答案

ThreadDeath的javadocs这样说:

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 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.


这是说应用程序应将ThreadDeath区别于 Exception (其他)Error类。
由于应该以不同的方式对待异常,因此使用不适合常规模式的类名称是有意义的。

但是,无论如何这应该具有历史意义。引发此问题的Thread.stop()方法在20年前已被弃用。您的代码不应使用stop(),因此无需将此异常与自身相关。而且你也不应该。

关于java - 为什么ThreadDeath类不称为ThreadDeathError呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65343116/

相关文章:

java - 比较两个 throwable 的正确方法是什么?

java - 如何使用 AudioSystem 在 Java 中播放音乐(.mp3)?

php - Yii2 错误处理程序在非对象调用时死得很丑

php - Symfony 控制台 : how to show line number when error/exception/warning occurs

javascript - 如何检测 Javascript 中未定义不起作用的引用错误?

java - 是什么导致没有详细信息引发java.lang.Error?

java - 断言错误和断言

java - 如何在不映射根路径的情况下配置springdoc ui?

java - 尝试从 SQLiteOpenHelper 检索数据时出现 Android NullpointerException

java - Hibernate 5.2 在我的表 postgres 上级联删除