错误和异常有什么区别? [英] What is difference between Errors and Exceptions?

查看:274
本文介绍了错误和异常有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

异常和错误之间的差异

我如何区分Java中的错误和异常?

How can I differentiate between Errors and Exceptions in Java?

推荐答案


错误表示严重问题
合理的申请应该是
不要抓住。

An Error "indicates serious problems that a reasonable application should not try to catch."

一个异常表示一个合理的应用程序可能
想要捕获的条件

An Exception "indicates conditions that a reasonable application might want to catch."

错误以及 RuntimeException &他们的子类是未选中的异常。所有其他异常类都是检查例外。

Error along with RuntimeException & their subclasses are unchecked exceptions. All other Exception classes are checked exceptions.

检查异常通常是一个程序可以恢复通过编程方式恢复这个例外可能是一个好主意。示例包括 FileNotFoundException ParseException 等等。程序员将通过使用try-catch块来检查这些异常或者将其返回给调用者

Checked exceptions are generally those from which a program can recover & it might be a good idea to recover from such exceptions programmatically. Examples include FileNotFoundException, ParseException, etc. A programmer is expected to check for these exceptions by using the try-catch block or throw it back to the caller

另一方面,我们有未选中的异常。这些是那些例外,如果一切顺利,可能不会发生,但它们确实发生。示例包括 ArrayIndexOutOfBoundException ClassCastException 等。许多应用程序将使用 try-catch throws 子句 RuntimeExceptions &他们的子类,但从语言的角度来看,不需要这样做。请注意,从 RuntimeException 恢复通常是可能的,但设计类/异常的人被认为是终端程序员检查这样的异常所必需的。

On the other hand we have unchecked exceptions. These are those exceptions that might not happen if everything is in order, but they do occur. Examples include ArrayIndexOutOfBoundException, ClassCastException, etc. Many applications will use try-catch or throws clause for RuntimeExceptions & their subclasses but from the language perspective it is not required to do so. Do note that recovery from a RuntimeException is generally possible but the guys who designed the class/exception deemed it unnecessary for the end programmer to check for such exceptions.

错误也是未选中的异常&程序员不需要做任何事情。事实上,对于错误使用 try-catch 子句是一个坏主意。大多数情况下,从错误中恢复是不可能的程序应该被允许终止。示例包括 OutOfMemoryError StackOverflowError 等。

Errors are also unchecked exception & the programmer is not required to do anything with these. In fact it is a bad idea to use a try-catch clause for Errors. Most often, recovery from an Error is not possible & the program should be allowed to terminate. Examples include OutOfMemoryError, StackOverflowError, etc.

请注意,虽然错误是未经检查的异常,我们不应该尝试处理它们,但是在代码中处理 RuntimeExceptions (也是未检查的异常)是可以的。检查的异常应由代码处理。

Do note that although Errors are unchecked exceptions, we shouldn't try to deal with them, but it is ok to deal with RuntimeExceptions(also unchecked exceptions) in code. Checked exceptions should be handled by the code.

这篇关于错误和异常有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆