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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:06:30  来源:igfitidea点击:

RuntimeException & Error

javaruntime-errorruntimeexception

提问by Roam

In the exceptions hierarchy, the descendants of RuntimeExceptionand those of Errorare runtime exceptions/errors.

在异常层次结构,后裔 RuntimeException和那些Error是运行时异常/错误。

The difference between the two is: Those under RuntimeExceptionare the ones caused by poor programming/design, and those of Errorare 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 RuntimeExceptionis extended.

对于在应用程序中编码异常,例如,在业务逻辑中发生某些事情时抛出异常,RuntimeException扩展了。

The question is, what exactly is the difference between extending RuntimeExceptionand extending Error-- except that extending Erroris bad practice?

问题是,扩展RuntimeException和扩展之间究竟有什么区别 Error——除了扩展 Error是不好的做法?

回答by Pita

Both Errorand RuntimeExceptionare unchecked exceptions, meaning that it indicate a flaw with the program, and usually should not be caught. (NullPointerException, IndexOutOfBoundsException, etc.)

两者ErrorRuntimeException没有勾选的异常,这意味着它表明有该程序的缺陷,而且通常不应该被捕获。(NullPointerExceptionIndexOutOfBoundsException等)

I think the main difference between the two is that RuntimeExceptionindicate there is a error with the program, and an Erroris something that is fatal but out of the program's control. (OutOfMemorryError, ThreadDeath, etc.)

我认为两者之间的主要区别在于RuntimeException表明程序存在错误,并且Error是致命但超出程序控制的东西。(OutOfMemorryErrorThreadDeath等)

Therefore subclassing an Erroris 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,它指出

Erroris the superclass of all the exceptions from which ordinary programs are not ordinarily expected to recover.

Error是普通程序通常不会从中恢复的所有异常的超类。

For RuntimeException, it states

对于RuntimeException,它指出

The class RuntimeExceptionis a direct subclass of Exception. RuntimeExceptionis 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 Exceptionis a super type of RuntimeException. But you will almost never see (I've never seen it)

作为一个包罗万象的案例,因为它ExceptionRuntimeException. 但你几乎永远不会看到(我从未见过)

try {
   ...
} catch (Error e) {
   ...
}