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 ofError
rather thanException
, even though it is a "normal occurrence", because many applications catch all occurrences ofException
and then discard the exception.
这是说应用程序应将
ThreadDeath
与区别于 Exception
和(其他)Error
类。由于应该以不同的方式对待异常,因此使用不适合常规模式的类名称是有意义的。
但是,无论如何这应该具有历史意义。引发此问题的
Thread.stop()
方法在20年前已被弃用。您的代码不应使用stop()
,因此无需将此异常与自身相关。而且你也不应该。
关于java - 为什么ThreadDeath类不称为ThreadDeathError呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65343116/