[英]why is java.lang.Throwable a class?
In java adjectives ending in -able are interfaces Serializable
, Comparable
etc... So why is Throwable
a class? 在java形容词中以-able结尾是接口
Serializable
, Comparable
等...那么为什么Throwable
是一个类? Wouldn't exception handling be easier if Throwable
were an interface? 如果
Throwable
是一个界面,异常处理会不会更容易? (Edit: eg Exception classes don't need to extend Exception/RuntimeException.) (编辑:例如,异常类不需要扩展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()
; 这不会避免
throw new Throwable()
的坏习惯;
Here's how James Gosling explained his decision: 以下是James Gosling解释他的决定:
Java Developer Connection Program : Why is
Throwable
not an interface?Java开发人员连接程序 :为什么
Throwable
不是接口? The name kind of suggests it should have been.这个名字有点暗示它应该是。 Being able to
catch
for types, that is, something liketry {} catch (<some interface or class>)
, instead of only classes.能够
catch
类型,即try {} catch (<some interface or class>)
,而不仅仅是类。 That would make [the] Java [programming language] much more flexible.这将使Java [编程语言]更加灵活。
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.James Gosling :
Throwable
和其他人不是接口的原因是因为我们决定,或者我很早就决定了。 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 aThrowable
thing.而且,如果你让
Throwable
成为一个界面,那么诱惑就是分配,使任何旧的对象成为一个Throwable
东西。 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.
它们不仅仅是一般的数据结构。
So why is Throwable a class?
那么为什么Throwable是一堂课呢?
I can think of two reasons: 我可以想到两个原因:
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.
我实际上怀疑如果他们必须根据任意接口而不是类层次结构来命令它们的catch块,它会使Java程序员更难。
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。
well Hashtable is also a concrete class! 好的Hashtable也是一个具体的课程! Something that can be hashted.
可以哈希的东西。
and what is Cloneable? 什么是克隆? it is not a correct English word.
这不是一个正确的英文单词。
FYI FYI
You can not use 你不能使用
void doSomething() throws Serializable
but you can use generics! 但你可以使用泛型!
<T extends Exception & Serializable> doSomething() throws T
Regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.