gpt4 book ai didi

java - 未经检查的异常或运行时异常之间的区别

转载 作者:IT老高 更新时间:2023-10-28 20:59:36 24 4
gpt4 key购买 nike

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

最佳答案

如其名称所述,未经检查的异常在编译时不检查,这意味着编译器不需要捕获或指定方法(使用抛出)它们。属于此类别的类在 11.2 Compile-Time Checking of Exceptions 部分中有详细说明。 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.

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

alt text

Error 类及其子类是普通程序通常不会从中恢复的异常,如 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.

总而言之,RuntimeExceptionunchecked exceptions 的子集,用于可以从中恢复的异常(但 unchecked exception 不是 RuntimeException 的同义词> 很多人都在这里回答)。

关于java - 未经检查的异常或运行时异常之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2699580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com