gpt4 book ai didi

java - 检查Java异常的定义?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:53:09 28 4
gpt4 key购买 nike

我看到一个定义可以是这样的:

Generally RuntimeExceptions are exceptions that can be prevented programmatically.

但这仍然不是检查异常的定义。我认为检查异常是“可以在编译时处理的异常”。这是正确的和/或你能告诉我更多吗?

我也在网站上看到了这个,你能解释一下引用吗?

Many people say that checked exceptions (i.e. these that you should explicitly catch or rethrow) should not be used at all.

Java: checked vs unchecked exception explanation

我可以只了解定义是什么吗?我也读到有些意外:

NumberFormatException is unchecked`

但我认为 NumberFormatException 已被检查,因为我会在编译时处理它。你能帮我理解吗?我已经完成了一些 Java 编程,但我从未编写过自己的异常类,我为什么需要它? enter image description here

更新

给出的定义是 Sierra/Bates 的 SCJP 书:

enter image description here

最佳答案

检查异常定义为 java.lang.Throwable 的任何子类(包括 Throwable 本身),它不是 java.lang.Error 的子类>java.lang.RuntimeException。您看到的指南只是指南,旨在帮助您理解运行时异常的意图。

请参阅 Java 语言规范,第 11.1.1 节

The unchecked exception classes are the runtime exception classes and the error classes.

The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are all subclasses of Throwable other than RuntimeException and its subclasses and Error and its subclasses.

关于java - 检查Java异常的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16132013/

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