2010-04-23 77 views
30

这是一个面试问题。由于两者都未被捕获,未经检查的异常和错误之间的主要区别是什么?他们将终止该计划。未经检查的异常或运行时异常之间的区别

+4

您的标题说明与问题不同的内容。你想知道RuntimeException和Error之间或者Unchecked和RuntimeException之间的区别吗? – 2010-04-23 15:09:33

+2

这不是* X **或** Y *之间的区别,但它是* X **和** Y *之间的区别。事实上,它是在未经检查的异常和运行时异常之间,还是在未经检查的异常和错误之间? – BalusC 2010-04-23 15:43:34

+0

看起来像这个相同的面试问题被问了很多:http://stackoverflow.com/questions/2693329/difference-between-errors-and-unchecked-exceptions-in-java – justkt 2010-04-23 16:21:56

回答

8

JavaDocs总结得很好。

java.lang.RuntimeException

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

java.lang.Error

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.

A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur.

注意, “未经检查的异常” 仅仅是一个RuntimeException的代名词。

+0

http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime .html 也提供了一个体面的differeneces描述 – ChadNC 2010-04-23 16:21:51

+1

我认为编写这个未经检查的异常是“RuntimeException”的同义词是误导(并且不正确)。 – 2010-04-23 16:49:20

+0

@Pascal,怎么样?我认为,每当有人在Java中引用“unchecked exception”时,他们引用RuntimeException的某个子类,不是吗? – 2010-04-23 17:22:16

1

RuntimeExceptions和像OutOfMemoryError这样的错误不需要被捕获并且可以被抛出,直到它们到达将终止应用程序的main()。

其他异常如果没有捕获或包含在抛出列表中,则会导致编译错误。

4

注:一个RuntimeException是一个未经检查的异常

一个未经检查的异常将是一个被称为是可能在执行过程中的一个点,但没有被捕获,例如一个NullPointerException始终是一个可能性,如果你不不检查它们,并会导致你的程序终止。你可以通过在try-catch中包装代码来检查它,但这不是强制执行的(不像强制异常以某种方式处理的检查异常)。

错误是指在执行过程中的任何时候都可能发生的错误,并且不能被真正捕获,因为它不是由特定的方法调用等引起的。例如OutOfMemoryError或StackOverflowError。这两种情况都可能在任何时候发生,并会导致您的应用程序终止。捕捉这些错误是毫无意义的,因为它们表明发生了某些事情,您无法从中恢复过来。

+4

不,这是相反的。运行时异常IS-A未经检查的异常。 – BalusC 2010-04-23 15:37:10

3

错误表示根本不应该发生的问题。如果遇到错误s.th.真的很糟糕发生。
另一方面,未经检查的异常用于每当可能预料到异常,但没有合理的方式来处理它,然后尝试catch语句将只是恼人的和浪费空间。

+2

RuntimeException是未经检查的异常的子集,而不是同义词。 – 2010-04-23 16:47:27

59

如前所述他们的名字,unchecked异常是在编译时这意味着编译器不要求的方法来捕获或指定(用throws)他们不检查。

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers. See §11.5 for a description of the exception class hierarchy and some of the exception classes defined by the Java API and Java virtual machine.

下图说明了异常层次结构::

alt text

Error及其子类是从普通的例外属于这一类的类在JLS的部分11.2 Compile-Time Checking of Exceptions详述程序通常不会恢复,正如11.5 The Exception Hierarchy中所解释的:

The class Error is a separate subclass of Throwable , distinct from Exception in the class hierarchy, to allow programs to use the idiom:

} catch (Exception e) { 

to catch all exceptions from which recovery may be possible without catching errors from which recovery is typically not possible.

总而言之,RuntimeException未检查的异常的子集可用于恢复的异常(但未检查的异常不是RuntimeException的同义词,正如许多人在此处回答的那样)。

1

错误和运行时异常统称为未经检查的异常。

运行时异常是应用程序内部的特殊情况,并且应用程序通常无法预测或从中恢复。这些通常表示编程错误,如逻辑错误或API使用不当等。您可能需要查看解释三种异常的链接。

http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html

我希望这有助于。

+1

尽管这个链接可能回答这个问题,但最好在这里包含答案的基本部分,并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。 – ivarni 2014-08-18 07:39:09

+1

@ivarni我更新了它,希望帮助:)感谢您的反馈。 – 2014-08-18 19:06:00

2

错误:这些是外部到应用程序异常情况,以及该应用程序通常不能预期或从恢复。

运行时异常:这些是内部到应用程序异常情况,以及该应用程序通常不能预期或从恢复。

您可能需要阅读this

1

的Checked Exception:

  • 扩展Throwable类除外RuntimeExceptionError的类被称为checked异常。
  • 也称为编译时异常,因为在编译时检查这些类型的异常。这意味着如果我们忽略这些异常(未使用try/catchthrow异常处理),则会发生编译错误。
  • 它们是由意外情况造成的代码之外的控制(例如数据库下来,文件I/O错误,输入错误等)
  • 我们可以通过try/catch块避免它们编程可恢复的问题。
  • 实施例:IOExceptionSQLException

未经检查的异常:

  • 延伸RuntimeException被称为未检查异常
  • 未检查异常不在编译时检查的类而是在运行时进行检查。并且这就是为什么它们也被称为“运行时异常”
  • 它们也是编程上可恢复的问题,但不像检查异常它们是由代码流或配置中的错误引起的。
  • 实施例:ArithmeticExceptionNullPointerExceptionArrayIndexOutOfBoundsException
  • 由于它们的编程错误,它们可以通过很好地/明智编码来避免。例如“除以零”发生ArithmeticEceeption。我们可以通过简单的条件来避免它们 - if(divisor!=0)。同样,我们可以通过简单地检查引用避免NullPointerException - if(object!=null)或使用甚至better techniques

错误:

  • Error是指那些没有被try/catch语句
  • 例处理无法挽回局面:OutOfMemoryError, VirtualMachineError,AssertionError