为什么 java.lang.Throwable 是一个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2890311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:18:44  来源:igfitidea点击:

why is java.lang.Throwable a class?

javaexception-handlingclass-design

提问by mdma

In java adjectives ending in -able are interfaces Serializable, Comparableetc... So why is Throwablea class? Wouldn't exception handling be easier if Throwablewere an interface? (Edit: e.g. Exception classes don't need to extend Exception/RuntimeException.)

在java中形容词-able结尾的接口SerializableComparable等等......那么,为什么是Throwable一类?如果Throwable是接口,异常处理不是更容易吗?(编辑:例如异常类不需要扩展异常/运行时异常。)

Obviously, changing it now is out the question. But could it be made abstract? Wouldn't that avoid the bad practice of throw new Throwable();

显然,现在改变它是不可能的。但它可以抽象吗?这难道不会避免throw new Throwable();的不良做法吗?

采纳答案by Brett Kail

So why is Throwable a class?

那么为什么 Throwable 是一个类呢?

I can think of two reasons:

我能想到两个原因:

  1. Exceptions have state. In particular, message, cause, and stack trace.
  2. It is easier for the JVM to implement efficient catch blocks. Class hierarchy checks are cheaper than interface checks.
  1. 异常有状态。特别是消息、原因和堆栈跟踪。
  2. JVM 更容易实现高效的 catch 块。类层次结构检查比接口检查便宜。

Wouldn't exception handling be easier if Throwable were an interface?

如果 Throwable 是一个接口,异常处理不是更容易吗?

Exception handling is a hard topic regardless of whether exceptions are classes or interfaces. I actually suspect it would make it harder on Java programmers if they have to order their catch blocks based on arbitrary interfaces rather than on class hierarchies.

无论异常是类还是接口,异常处理都是一个难题。我实际上怀疑如果 Java 程序员必须根据任意接口而不是类层次结构来排序它们的 catch 块,这会让他们更难。

But could it be made abstract?

但它可以抽象吗?

In theory, yes. In practice, no. Too much code depends on being able to create an instance of Throwable in order to call getStackTrace.

理论上,是的。在实践中,没有。太多的代码取决于能够创建 Throwable 的实例以调用 getStackTrace。

回答by polygenelubricants

Here's how James Gosling explained his decision:

以下是詹姆斯高斯林如何解释他的决定:

Java Developer Connection Program: Why is Throwablenot an interface? The name kind of suggests it should have been. Being able to catchfor types, that is, something like try {} catch (<some interface or class>), instead of only classes. That would make [the] Java [programming language] much more flexible.

James Gosling: The reason that the Throwableand the rest of those guys are not interfaces is because we decided, or I decided fairly early on. I decided that I wanted to have some state associated with every exception that gets thrown. And you can't do that with interfaces; you can only do that with classes. The state that's there is basically standard. There's a message, there's a snapshot, stuff like that — that's always there. and also, if you make Throwablean interface the temptation is to assign, to make any old object be a Throwablething. It feels stylistically that throwing general objects is probably a bad idea, that the things you want to throw really ought to be things that are intended to be exceptions that really capture the nature of the exception and what went on. They're not just general data structures.

Java Developer Connection Program:为什么Throwable不是接口?这个名字暗示它应该是。能够catch为类型,即类似于try {} catch (<some interface or class>),而不仅仅是类。这将使 [the] Java [programming language] 更加灵活。

James GoslingThrowable那些人和其他人不是接口的原因是因为我们决定,或者我很早就决定了。我决定让一些状态与抛出的每个异常相关联。而你不能用接口来做到这一点;你只能用类来做到这一点。那里的状态基本上是标准的。有一条消息,有一张快照,诸如此类的东西——它一直都在。而且,如果你制作Throwable一个接口,诱惑就是分配,让任何旧对象成为一个Throwable事物。从风格上来说,抛出一般对象可能是一个坏主意,你想要抛出的东西真的应该是旨在成为真正捕捉异常本质和发生了什么的异常的东西。它们不仅仅是通用的数据结构。

References

参考

回答by irreputable

well Hashtable is also a concrete class! Something that can be hashted.

好吧 Hashtable 也是一个具体的类!可以散列的东西。

and what is Cloneable? it is not a correct English word.

什么是可克隆?它不是一个正确的英文单词。

回答by Grim

FYI

供参考

You can not use

你不能使用

void doSomething() throws Serializable

but you can use generics!

但是你可以使用泛型!

<T extends Exception & Serializable> doSomething() throws T

Regards

问候