未经检查的异常和运行时异常之间的区别
这是一个面试问题。未经检查的异常和错误之间的主要区别是什么,因为两者都没有被捕获?他们将终止该计划。
This was an interview question. What is the main difference between unchecked exception and error as both are not caught? They will terminate the program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
正如其名称所示,未检查异常在编译时不进行检查,这意味着编译器不需要捕获或指定方法(使用
抛出
)它们。属于此类别的类在 11.2 节中详细介绍JLS 异常的编译时检查:下图说明了异常层次结构:
类
Error
及其子类是普通程序通常不希望从中恢复的异常,如 11.5 异常层次结构:总而言之,
RuntimeException
是未检查异常的一个子集,用于可以从中恢复的异常(但未检查异常并不是RuntimeException
的同义词,因为许多异常都可以从该异常中恢复)在这里回答)。As stated by their name, unchecked exceptions are not checked at compile-time which means that the compiler doesn't require methods to catch or to specify (with a
throws
) them. Classes belonging to this category are detailed in the section 11.2 Compile-Time Checking of Exceptions of the JLS:The following picture illustrates the Exception hierarchy:
The class
Error
and its subclasses are exceptions from which ordinary programs are not ordinarily expected to recover and, as explained in 11.5 The Exception Hierarchy:To summarize,
RuntimeException
are a subset of unchecked exceptions for exceptions from which recovery is possible (but unchecked exception is not a synonym ofRuntimeException
as many are answering here).JavaDocs 很好地总结了这些。
java.lang.RuntimeException
< /a>:java.lang.Error
< /a>:请注意,“未经检查的异常”仅仅是 RuntimeException 的同义词。
The JavaDocs sum these up pretty well.
java.lang.RuntimeException
:java.lang.Error
:Note that "unchecked exception" is merely a synonym for a
RuntimeException
.注意: RuntimeException 是一种未经检查的异常
未经检查的异常是已知在执行过程中的某一点可能发生但未被捕获的异常,例如,如果您不检查它们,则始终有可能出现 NullPointerException,并且会导致你的程序要终止。您可以通过将代码包装在 try-catch 中来检查它,但这不是强制执行的(与检查异常不同,它会强制以某种方式处理异常)。
错误是在执行过程中的任何时刻都可能发生的错误,并且无法真正被捕获,因为它不是由特定方法调用等显式引起的。例如 OutOfMemoryError 或 StackOverflowError。这两种情况都可能随时发生,并会导致您的应用程序终止。捕获这些错误是没有意义的,因为它们表明发生了一些您将无法恢复的事情。
Note: a RuntimeException IS an unchecked exception
An unchecked exception would be one that is known to be possible at a point in the execution but is not caught, for example a NullPointerException is always a possibility if you don't check for them and will cause your program to terminate. You could check for it by wrapping code in try-catch, but this is not enforced (unlike a checked exception that will enforce that the exception is handled in some way).
An error is something that can occur at any point during execution and can't really be caught because it is not eplicitly caused by a particular method call etc. For example an OutOfMemoryError or a StackOverflowError. Both of these could occur at any time and will cause your application to terminate. Catching these errors make no sense as they indicate that something has happened that you won't be able to recover from.
错误表示根本不应该发生的问题。如果您遇到错误……真的很糟糕的事情发生了。
另一方面,当可以以某种方式预期异常但没有合理的方法来处理它时,就会使用未检查异常(运行时异常),因此 try catch 语句会很烦人和浪费的空间。
Errors indicate fundamental problems that should never occur. If you run into an error s.th. really bad happened.
Unchecked Exceptions (Runtime Exceptions) on the other hand are used whenever an exception could be expected somehow but there is no reasonable way to deal with it then and thus a try catch statement would be just annoying and a waste of space.
受检查异常:
Throwable
类(RuntimeException
和Error
除外)的类称为受检查异常。IOException
、SQLException
等Unchecked Exception:
RuntimeException
的类是称为未经检查的异常ArithmeticException
、NullPointerException
、ArrayIndexOutOfBoundsException
等ArithmeticEceeption
。我们可以通过一个简单的 if 条件来避免它们 -if(divisor!=0)
。同样,我们可以通过简单地检查引用 -if(object!=null)
或什至使用 更好的技术错误:
Error
是指 try/catch 未处理的不可恢复情况OutOfMemoryError
、VirtualMachineError
、AssertionError
代码>等Checked Exception:
Throwable
class exceptRuntimeException
andError
are known as checked exceptions.try/catch
orthrow
the exception) then a compilation error occurred.try/catch
block.IOException
,SQLException
etcUnchecked Exception:
RuntimeException
are known as unchecked exceptionsArithmeticException
,NullPointerException
,ArrayIndexOutOfBoundsException
etcArithmeticEceeption
. We can avoid them by a simple if condition -if(divisor!=0)
. Similarly we can avoidNullPointerException
by simply checking the references -if(object!=null)
or using even better techniquesError:
Error
refers irrecoverable situation that are not being handled by try/catchOutOfMemoryError
,VirtualMachineError
,AssertionError
etc.错误:这些是应用程序外部的异常情况,应用程序通常无法预测或恢复。
运行时异常:这些是应用程序内部的异常情况,应用程序通常无法预测或恢复。
您可能想阅读此:
Error: These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from.
Runtime exception : These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from.
You may want to read this :
RuntimeExceptions 和诸如 OutOfMemoryError 之类的错误不需要被捕获,并且可以抛出,直到它们到达 main(),这将终止应用程序。
其他异常如果未捕获或未包含在抛出列表中,则会导致编译错误。
RuntimeExceptions
and Errors likeOutOfMemoryError
don't need to be catched and can be thrown until they reach main() which will terminate the application.Other Exceptions cause an compile error if they are not catched or included in the throws list.
错误和运行时异常统称为未经检查的异常。
运行时异常是应用程序内部的异常情况,应用程序通常无法预测或恢复。这些通常表明编程错误,例如逻辑错误或 API 使用不当。
您可能需要查看此链接,其中解释了三种异常。
http://docs.oracle.com/javase/tutorial/essential/exceptions /catchOrDeclare.html
我希望这会有所帮助。
Errors and runtime exceptions are collectively known as unchecked exceptions.
runtime exceptions are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API
You may want to take a look at this link which explains the Three Kinds of Exceptions.
http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html
I hope this helps.
java.lang.Error 和 java.lang.Exception 都是 java.lang.Throwable 的子类。
java.lang.Error 类表示主要由应用程序运行环境引起的错误。例如,当 JVM 内存不足时会发生 OutOfMemoryError,或者当堆栈溢出时会发生 StackOverflowError。
其中java.lang.Exception类代表主要由应用程序本身引起的异常。例如,当应用程序尝试访问 null 对象时,会发生 NullPointerException;当应用程序尝试转换不兼容的类类型时,会发生 ClassCastException。
java.lang.Exception 的所有子类(除了 RunTimeException 的子类)都是检查异常。例如,FileNotFoundException、IOException、SQLException、ClassNotFoundException 等...
java.lang.RuntimeException 和 java.lang.Error 的所有子类都是未经检查的异常。例如,NullPointerException、ArithmeticException、ClassCastException、ArrayIndexOutOfBoundsException、StackOverflowError、OutOfMemoryError 等...
来源:错误与异常,检查与未检查异常
Both java.lang.Error and java.lang.Exception are the sub classes of java.lang.Throwable.
java.lang.Error class represents the errors which are mainly caused by the environment in which application is running. For example, OutOfMemoryError occurs when JVM runs out of memory or StackOverflowError occurs when stack overflows.
Where as java.lang.Exception class represents the exceptions which are mainly caused by the application itself. For example, NullPointerException occurs when an application tries to access null object or ClassCastException occurs when an application tries to cast incompatible class types.
All the sub classes of java.lang.Exception (except sub classes of RunTimeException) are checked exceptions. For example, FileNotFoundException, IOException, SQLException, ClassNotFoundException etc…
All the sub classes of java.lang.RuntimeException and java.lang.Error are unchecked exceptions. For example, NullPointerException, ArithmeticException, ClassCastException, ArrayIndexOutOfBoundsException, StackOverflowError, OutOfMemoryError etc…
Source : Error Vs Exceptions, Checked Vs Unchecked Exceptions