Difference between Unchecked exception or runtime exception(未经检查的异常或运行时异常之间的区别)
问题描述
这是一道面试题.未经检查的异常和错误之间的主要区别是什么,因为两者都没有被捕获?他们将终止程序.
正如他们的名字所说,unchecked exceptions not check at compile-time 这意味着编译器不需要方法来捕获或指定(使用 throws)它们.属于此类别的类在 :
Error 类是一个单独的Throwable 的子类,区别于Exception 在类层次结构中,以允许程序使用成语:
} 捕获(异常 e){捕获所有异常没有恢复可能是可能的捕获从中恢复的错误通常是不可能的.
总而言之,RuntimeException 是 unchecked exceptions 的子集,用于可以从中恢复的异常(但 unchecked exception 不是 RuntimeException 很多人都在这里回答).
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.
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
RuntimeExceptionand its subclasses, and the classErrorand 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:
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
Erroris a separate subclass ofThrowable, distinct fromExceptionin 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).
这篇关于未经检查的异常或运行时异常之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:未经检查的异常或运行时异常之间的区别
基础教程推荐
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- JPA惰性列表上的流 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
