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

发布于 2024-09-02 08:59:31 字数 297 浏览 7 评论 0原文

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

显然,现在更改它是不可能的。但它能变得抽象吗?这不是可以避免 抛出 new Throwable(); 的不良做法吗?

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

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蛮可爱 2024-09-09 08:59:31

詹姆斯·高斯林是这样解释他的决定的:

Java 开发人员连接计划:为什么 Throwable 不是一个接口?这个名字有点暗示它应该是这样。能够catch类型,即try {} catch (),而不仅仅是类。这将使 Java [编程语言]更加灵活。

James GoslingThrowable 和其他这些东西不是接口的原因是因为我们决定,或者我很早就决定了。我决定希望有一些状态与抛出的每个异常相关联。你不能用接口来做到这一点;你只能通过课程来做到这一点。那里的状态基本上是标准的。有一条消息,有一个快照,诸如此类的东西 - 总是存在的。而且,如果您将 Throwable 设为接口,那么很可能会进行分配,以使任何旧对象成为 Throwable 事物。从风格上来说,抛出一般对象可能是一个坏主意,您想要抛出的东西实际上应该是那些真正捕获异常本质和所发生情况的异常的东西。它们不仅仅是一般的数据结构。

参考文献

Here's how James Gosling explained his decision:

Java Developer Connection Program: Why is Throwable not an interface? The name kind of suggests it should have been. Being able to catch for 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 Throwable and 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 Throwable an interface the temptation is to assign, to make any old object be a Throwable thing. 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.

References

梦罢 2024-09-09 08:59:31

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

我可以想到两个原因:

  1. 异常有状态。特别是消息、原因和堆栈跟踪。
  2. JVM 更容易实现高效的 catch 块。类层次结构检查比接口检查便宜。

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

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

但是它可以变得抽象吗?

理论上是的。实际上,没有。太多代码依赖于能够创建 Throwable 实例来调用 getStackTrace。

So why is Throwable a class?

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.

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

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.

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.

往日情怀 2024-09-09 08:59:31

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

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

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

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

不甘平庸 2024-09-09 08:59:31

仅供参考,

您不能使用泛型

void doSomething() throws Serializable

,但可以使用泛型!

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

问候

FYI

You can not use

void doSomething() throws Serializable

but you can use generics!

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

Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文