java java中错误和未经检查的异常之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2693329/
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
difference between errors and unchecked exceptions in java?
提问by GuruKulki
As we know if any error or any unchecked exception occurs then our program will halt, then what are the differences between those?
我们知道如果发生任何错误或任何未经检查的异常,那么我们的程序将停止,那么它们之间有什么区别?
回答by Eric Eijkelenboom
In short:
简而言之:
You can, and probably should, recover from an exception.
您可以而且可能应该从异常中恢复。
You can, but should not, recover from an error.
您可以但不应该从错误中恢复。
回答by justkt
From the ErrorJavadoc:
从错误Javadoc:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.
Error 是 Throwable 的一个子类,它指示合理的应用程序不应尝试捕获的严重问题。大多数此类错误是异常情况。ThreadDeath 错误虽然是“正常”情况,但也是 Error 的一个子类,因为大多数应用程序不应该尝试捕获它。
Versus the ExceptionJavadoc
与异常Javadoc
The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
Exception 类及其子类是 Throwable 的一种形式,它指示合理的应用程序可能想要捕获的条件。
So, even though an unchecked exception is not required to be caught, you may want to. An error, you don't want to catch.
因此,即使不需要捕获未经检查的异常,您也可能想要捕获。一个错误,你不想抓住。
回答by Razib
Unchecked Exception:
未经检查的异常:
- The classes that extend
RuntimeExceptionare known as unchecked exceptions - Unchecked exceptions are not checked at compile-time rather they are checked at runtime.And thats why they are also called "Runtime Exception"
- They are also programmatically recoverable problems but unlike checked exceptionthey are caused by faults in code flow or configuration.
- Example:
ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsExceptionetc - Since they are programming error, they can be avoided by nicely/wisely coding. For example "dividing by zero" occurs
ArithmeticEceeption. We can avoid them by a simple if condition -if(divisor!=0). Similarly we can avoidNullPointerExceptionby simply checking the references -if(object!=null)or using even better techniques
- 扩展的类
RuntimeException称为未经检查的异常 - 未经检查的异常不在编译时检查,而是在运行时检查。这就是为什么它们也被称为“运行时异常”
- 它们也是可通过编程方式恢复的问题,但与检查异常不同,它们是由代码流或配置中的错误引起的。
- 例如:
ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException等 - 由于它们是编程错误,因此可以通过良好/明智的编码来避免它们。例如,发生“除以零”
ArithmeticEceeption。我们可以通过一个简单的 if 条件来避免它们 -if(divisor!=0)。同样,我们可以NullPointerException通过简单地检查引用来避免-if(object!=null)或者使用更好的技术
Error:
错误:
Errorrefers irrecoverable situation that are not being handled by try/catchExample:
OutOfMemoryError,VirtualMachineError,AssertionErroretc.This question may also be helpful in this context - Runtime/Checked/Unchecked/Error-Exception
Error指的是 try/catch 没有处理的不可恢复的情况例如:
OutOfMemoryError,VirtualMachineError,AssertionError等。这个问题在这种情况下也可能有帮助 - Runtime/Checked/Unchecked/Error-Exception
回答by Santiago Ruiz
The term ?reasonable? is relative. As it is, also, the term ?application?
术语?合理的?是相对的。同样,术语?应用程序?
As middleware developer, I'm used to deal with unchecked exceptions thrown, or simply ignored, by application developers
作为中间件开发人员,我习惯于处理应用程序开发人员抛出或简单忽略的未经检查的异常
What is reasonable to be catched by an application is is a subset of what is reasonable to be catched by its underlying infrastructure (that is, itself, a kind of application)
应用程序合理捕获的是其底层基础设施(即,其本身,一种应用程序)合理捕获的子集
That's where unchecked exceptions are different from errors. An unchecked can be catched by the infrastructure (i.e. to be registered in a.database) but ignored by an application
这就是未经检查的异常与错误不同的地方。基础设施可以捕获未经检查的对象(即要在.database 中注册)但会被应用程序忽略
Registering an error could be impossible because there could be no JVM to run the registering code (or even the catching code), that's why it's not reasonable to catch them
注册错误可能是不可能的,因为可能没有 JVM 来运行注册代码(甚至是捕获代码),这就是为什么捕获它们是不合理的
As an aside, checked exceptions are IMHO overused. Catching them to promote to runtime exceptions is too much usual
顺便说一句,检查异常恕我直言过度使用。捕获它们以提升运行时异常太常见了
回答by Bozhidar Batsov
Just as checked exceptions are useful for signaling when your methods cannot fulfill their contract, there are other errors outside of your control that can occur that prevent the Java virtual machine from fulfilling its specification, such as when memory is exhausted. Since you can't plan for such errors ahead of time, you would have to catch them everywhere, which defeats the principle of maintaining uncluttered code. Therefore, these errors are unchecked exceptions, meaning exceptions that you don't have to include in a throws clause. You are welcome to catch them (well, some of them), but the compiler won't make you do it. Unchecked exceptions fall into two categories: those that extend RuntimeException, and those that extend Error. I realize that I said earlier that classes inheriting from class Exception are checked exceptions, but that's only half true: the whole truth is that classes in the Exception hierarchy other than those in the RuntimeException sub-hierarchy are checked exceptions.
正如检查异常对于在您的方法无法履行其契约时发出信号很有用一样,还有可能发生在您的控制之外的其他错误会阻止 Java 虚拟机履行其规范,例如当内存耗尽时。由于您无法提前计划此类错误,因此您必须到处捕捉它们,这违背了保持代码整洁的原则。因此,这些错误是未经检查的异常,这意味着您不必包含在 throws 子句中的异常。欢迎您捕获它们(好吧,其中一些),但编译器不会让您这样做。未经检查的异常分为两类:扩展 RuntimeException 的异常和扩展 Error 的异常。我意识到我之前说过从类 Exception 继承的类是检查异常,
Exceptions that extend RuntimeException represent errors that you may want to handle, although you're not required to.
扩展 RuntimeException 的异常表示您可能想要处理的错误,尽管您不是必需的。
As I stated above, RuntimeExceptions are not checked because making you advertise them would have no effect on establishing the correctness of your methods, and would unnecessarily clutter your otherwise very readable code. Exceptions derived from the Error class, on the other hand, are unchecked because you never want to catch them! Error exceptions are severe errors that require shutting down the virtual machine. InternalError, which I used above, extends VirtualMachineError, which is an Error subclass. OutOfMemoryError is another obvious severe error, but there are others, like StackOverflowError, and various LinkageErrors. A linkage error means something has gone amiss when the class loader tried to load a class for execution, and commonly occurs either because some external source has introduced malicious code in an attempt to circumvent Java's security mechanism, or it came from an out-of-spec byte code generator.
正如我上面所说的,RuntimeExceptions 不会被检查,因为让你公布它们不会影响你的方法的正确性,并且会不必要地混乱你原本非常易读的代码。另一方面,从 Error 类派生的异常是未经检查的,因为您永远不想捕获它们!错误异常是需要关闭虚拟机的严重错误。我在上面使用的 InternalError 扩展了 VirtualMachineError,它是一个 Error 子类。OutOfMemoryError 是另一个明显的严重错误,但还有其他错误,如 StackOverflowError 和各种 LinkageErrors。链接错误意味着当类加载器试图加载一个类以供执行时出现问题,
回答by Avi
From the JavaDoc:
来自 JavaDoc:
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
Error 是 Throwable 的一个子类,它指示合理的应用程序不应尝试捕获的严重问题。
RuntimeException 是那些可以在 Java 虚拟机正常运行期间抛出的异常的超类。
So, the only difference technically is that they are two different classes. You will only catch both if you declare
因此,技术上唯一的区别是它们是两个不同的类。如果你声明,你只会抓住两者
catch (Throwable e) { }
But there is a big difference in how they are intended to be used. Unchecked exceptions (RuntimeExceptions) are intended to deal with programming errors and other unexpected problems, but shouldbe caught and handled in the application. Errors are intended to represent problems that the program cannot deal with, such as running out of memory.
但是它们的用途有很大的不同。未经检查的异常 ( RuntimeExceptions) 旨在处理编程错误和其他意外问题,但应在应用程序中捕获和处理。错误旨在表示程序无法处理的问题,例如内存不足。
回答by Mukeshkoshym
Difference between errors and unchecked exceptions in java?
java中错误和未经检查的异常之间的区别?
Unchecked exceptions = class RuntimeException and its subclasses + class Error and its subclasses
未经检查的异常 = 类 RuntimeException 及其子类 + 类 Error 及其子类
There fore the error are part of unchecked exception. Unchecked exception also contains RuntimeException and its subclasses.
因此错误是未经检查的异常的一部分。未经检查的异常还包含 RuntimeException 及其子类。

