gpt4 book ai didi

java - RuntimeException 和异常行为

转载 作者:行者123 更新时间:2023-11-29 03:27:50 24 4
gpt4 key购买 nike

我的问题是为什么 JVM 在执行期间抛出 RuntimeException 不像Exception ,它在编译时抛出,即使 RuntimeException extends Exception

这种行为差异在哪里以及如何定义?

最佳答案

JavaDoc说:-

11.1.1. The Kinds of Exceptions

The class RuntimeException is a direct subclass of Exception.RuntimeException is the superclass of all the exceptions which may bethrown for many reasons during expression evaluation, but from whichrecovery may still be possible.

RuntimeException and all its subclasses are, collectively, the run-time exception classes.

11.3. Run-Time Handling of an Exception

When an exception is thrown (§14.18), control is transferred from thecode that caused the exception to the nearest dynamically enclosingcatch clause, if any, of a try statement (§14.20) that can handle theexception.

A statement or expression is dynamically enclosed by a catch clause ifit appears within the try block of the try statement of which thecatch clause is a part, or if the caller of the statement orexpression is dynamically enclosed by the catch clause.

关于java - RuntimeException 和异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20075113/

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