未经检查的异常和运行时异常之间的区别

发布于 2024-08-29 15:45:17 字数 57 浏览 4 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦过后 2024-09-05 15:45:17

正如其名称所示,未检查异常在编译时不进行检查,这意味着编译器不需要捕获或指定方法(使用 抛出)它们。属于此类别的类在 11.2 节中详细介绍JLS 异常的编译时检查

未经检查的异常类是类RuntimeException及其子类,以及类Error及其子类 。所有其他异常类都是检查异常类。 Java API 定义了许多异常类,包括检查的和未检查的。程序员可以声明其他异常类(包括检查的和未检查的)。有关以下说明,请参阅 §11.5异常类层次结构以及 Java API 和 Java 虚拟机定义的一些异常类。

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

alt text

Error及其子类是普通程序通常不希望从中恢复的异常,如 11.5 异常层次结构

Error是一个单独的
Throwable 的子类,不同于
类层次结构中的Exception,以
允许程序使用惯用语:

} catch (异常 e) {

捕获所有异常
恢复可能无需
捕获并从中恢复的错误
通常不可能。

总而言之,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 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.

The following picture illustrates the Exception hierarchy:

alt text

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:

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.

To summarize, RuntimeException are a subset of unchecked exceptions for exceptions from which recovery is possible (but unchecked exception is not a synonym of RuntimeException as many are answering here).

美羊羊 2024-09-05 15:45:17

JavaDocs 很好地总结了这些。

java.lang.RuntimeException< /a>:

RuntimeException 是 Java 虚拟机正常运行期间可能抛出的异常的超类。

方法不需要在其 throws 子句中声明在方法执行期间可能抛出但未被捕获的 RuntimeException 的任何子类。

java.lang.Error< /a>:

Error 是 Throwable 的子类,它指示合理的应用程序不应尝试捕获的严重问题。大多数此类错误都是异常情况。 ThreadDeath 错误虽然是“正常”情况,但也是 Error 的子类,因为大多数应用程序不应尝试捕获它。

方法不需要在其 throws 子句中声明在方法执行期间可能抛出但未被捕获的任何 Error 子类,因为这些错误是永远不应该发生的异常情况。

请注意,“未经检查的异常”仅仅是 RuntimeException 的同义词。

The JavaDocs sum these up pretty well.

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.

Note that "unchecked exception" is merely a synonym for a RuntimeException.

半透明的墙 2024-09-05 15:45:17

注意: 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.

旧夏天 2024-09-05 15:45:17

错误表示根本不应该发生的问题。如果您遇到错误……真的很糟糕的事情发生了。
另一方面,当可以以某种方式预期异常但没有合理的方法来处理它时,就会使用未检查异常(运行时异常),因此 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.

爱的故事 2024-09-05 15:45:17

受检查异常:

  • 扩展 Throwable 类(RuntimeExceptionError 除外)的类称为受检查异常。
  • 也称为编译时异常,因为这些类型的异常是在编译时检查的。这意味着如果我们忽略这些异常(不使用 try/catch 处理或抛出异常),则会发生编译错误。
  • 它们是以编程方式恢复的问题,是由代码控制之外的意外情况引起的(例如数据库关闭、文件 I/O 错误、错误输入等),
  • 我们可以使用 try/catch 块来避免它们。
  • 示例: IOExceptionSQLException

Unchecked Exception:

  • 扩展 RuntimeException 的类是称为未经检查的异常
  • 未经检查的异常在编译时不会检查,而是在运行时检查。这就是为什么它们也称为“运行时异常”
  • 它们也是以编程方式可恢复的问题,但与检查的异常不同异常 它们是由代码流或配置中的错误引起的。
  • 示例: ArithmeticExceptionNullPointerExceptionArrayIndexOutOfBoundsException
  • 由于它们是编程错误,因此可以通过很好/明智地避免编码。例如,“除以零”发生ArithmeticEceeption。我们可以通过一个简单的 if 条件来避免它们 - if(divisor!=0)。同样,我们可以通过简单地检查引用 - if(object!=null) 或什至使用 更好的技术

错误:

  • Error 是指 try/catch 未处理的不可恢复情况
  • 示例: OutOfMemoryErrorVirtualMachineErrorAssertionError代码>等

Checked Exception:

  • The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions.
  • Also known as compile time exception because these type of exceptions are checked at compile time. That means if we ignore these exception (not handled with try/catch or throw the exception) then a compilation error occurred.
  • They are programmatically recoverable problems which are caused by unexpected conditions outside control of code (e.g. database down, file I/O error, wrong input, etc)
  • We can avoid them using try/catch block.
  • Example: IOException, SQLException etc

Unchecked Exception:

  • The classes that extend RuntimeException are 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 exception they are caused by faults in code flow or configuration.
  • Example: ArithmeticException,NullPointerException, ArrayIndexOutOfBoundsException etc
  • 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 avoid NullPointerException by simply checking the references - if(object!=null) or using even better techniques

Error:

  • Error refers irrecoverable situation that are not being handled by try/catch
  • Example: OutOfMemoryError, VirtualMachineError, AssertionError etc.
空城旧梦 2024-09-05 15:45:17

错误:这些是应用程序外部的异常情况,应用程序通常无法预测或恢复。

运行时异常:这些是应用程序内部的异常情况,应用程序通常无法预测或恢复。

您可能想阅读

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 :

若无相欠,怎会相见 2024-09-05 15:45:17

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

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

RuntimeExceptions and Errors like OutOfMemoryError 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.

请持续率性 2024-09-05 15:45:17

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

运行时异常是应用程序内部的异常情况,应用程序通常无法预测或恢复。这些通常表明编程错误,例如逻辑错误或 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.

感情洁癖 2024-09-05 15:45:17

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文