Java 运行时异常和错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20461688/
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
RuntimeException & Error
提问by Roam
In the exceptions hierarchy, the descendants of
RuntimeException
and those of Error
are runtime exceptions/errors.
在异常层次结构,后裔
RuntimeException
和那些Error
是运行时异常/错误。
The difference between the two is: Those under RuntimeException
are
the ones caused by poor programming/design, and those of Error
are
the ones that can't/shouldn't be controlled by the developer.
两者的区别在于:下面的RuntimeException
是编程/设计不良导致的,而下面的Error
是开发人员无法/不应控制的。
For coding an exception within the application,
for instance, to throw an exception when something in the business logic occurs,
the RuntimeException
is extended.
对于在应用程序中编码异常,例如,在业务逻辑中发生某些事情时抛出异常,RuntimeException
扩展了。
The question is, what exactly is the difference between extending
RuntimeException
and extending Error
-- except that extending
Error
is bad practice?
问题是,扩展RuntimeException
和扩展之间究竟有什么区别
Error
——除了扩展
Error
是不好的做法?
回答by Pita
Both Error
and RuntimeException
are unchecked exceptions, meaning that it indicate a flaw with the program, and usually should not be caught. (NullPointerException
, IndexOutOfBoundsException
, etc.)
两者Error
并RuntimeException
没有勾选的异常,这意味着它表明有该程序的缺陷,而且通常不应该被捕获。(NullPointerException
,IndexOutOfBoundsException
等)
I think the main difference between the two is that RuntimeException
indicate there is a error with the program, and an Error
is something that is fatal but out of the program's control. (OutOfMemorryError
, ThreadDeath
, etc.)
我认为两者之间的主要区别在于RuntimeException
表明程序存在错误,并且Error
是致命但超出程序控制的东西。(OutOfMemorryError
,ThreadDeath
等)
Therefore subclassing an Error
is bad practice because an error is usually not something that could be fixed by your program at runtime. In your program, should you need to throw something, use an Exception
.
因此子类化 anError
是不好的做法,因为错误通常不是您的程序在运行时可以修复的。在您的程序中,如果您需要抛出一些东西,请使用Exception
.
回答by Sotirios Delimanolis
The Q is, what exactly is the difference between extending RuntimeException and extending Error-- except that extending Error is bad practice?
Q 是,扩展 RuntimeException 和扩展 Error 之间究竟有什么区别——除了扩展 Error 是不好的做法?
You've already mentioned the main differences. The Java Language Specification says the same thing in different terms. For Error
, it states
您已经提到了主要区别。Java 语言规范用不同的术语表述同一件事。对于Error
,它指出
Error
is the superclass of all the exceptions from which ordinary programs are not ordinarily expected to recover.
Error
是普通程序通常不会从中恢复的所有异常的超类。
For RuntimeException
, it states
对于RuntimeException
,它指出
The class
RuntimeException
is a direct subclass of Exception.RuntimeException
is the superclass of all the exceptions which may be thrown for many reasons during expression evaluation, but from which recovery may still be possible.
该类
RuntimeException
是 Exception 的直接子类。RuntimeException
是所有异常的超类,在表达式计算过程中可能由于多种原因抛出这些异常,但仍然可以从中 恢复。
What you should take away from these quotes is that you will commonly see
你应该从这些引文中得到的是,你会经常看到
try {
...
} catch (Exception e) { // catches RuntimeException
...
}
as a catch all case since Exception
is a super type of RuntimeException
. But you will almost never see (I've never seen it)
作为一个包罗万象的案例,因为它Exception
是RuntimeException
. 但你几乎永远不会看到(我从未见过)
try {
...
} catch (Error e) {
...
}