Files
logseq-1/journals/2024_06_28.md

10 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
- [Exception 和 Error 有什么区别?](https://javaguide.cn/java/basis/java-basic-questions-03.html#exception-%E5%92%8C-error-%E6%9C%89%E4%BB%80%E4%B9%88%E5%8C%BA%E5%88%AB) #card #java
id:: 667e7553-cb20-45d8-935e-39dfa35eb667
- 在 Java 中,所有的异常都有一个共同的祖先 `java.lang` 包中的 `Throwable` 类。`Throwable` 类有两个重要的子类:
- **`Exception`** : 程序本身可以处理的异常,可以通过 `catch` 来进行捕获。`Exception` 又可以分为 Checked Exception (受检查异常,必须处理) 和 Unchecked Exception (不受检查异常,可以不处理)。
- **`Error`**`Error` 属于程序无法处理的错误 ~~我们没办法通过 `catch` 来进行捕获~~不建议通过 `catch` 捕获 。例如 Java 虚拟机运行错误(`Virtual MachineError`)、虚拟机内存不够错误 (`OutOfMemoryError`)、类定义错误(`NoClassDefFoundError`)等 。这些异常发生时Java 虚拟机JVM一般会选择线程终止。
- Checked Exception 和 Unchecked Exception 有什么区别?
- Checked Exception 即 受检查异常 Java 代码在编译过程中,如果受检查异常没有被 catch 或者 throws 关键字处理的话,就没办法通过编译。比如下面这段 IO 操作的代码:除了 RuntimeException 及其子类以外,其他的 Exception 类及其子类都属于受检查异常 。
- 常见的受检查异常有IO 相关的异常、ClassNotFoundException、SQLException...。
- Unchecked Exception 即 不受检查异常 Java 代码在编译过程中 ,我们即使不处理不受检查异常也可以正常通过编译。
-