gpt4 book ai didi

java - 编译器如何识别存在已检查异常

转载 作者:行者123 更新时间:2023-11-29 07:14:46 24 4
gpt4 key购买 nike

在 Java 中,编译器如何识别存在已检查的异常?我的意思是它是如何识别它的?

最佳答案

直接来自文档:

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (§8.4.6) or constructor (§8.8.5) must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.

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.

所以基本上它会查看代码,如果遇到异常,就会查找异常的继承层次结构以决定是否检查它。

Read

关于java - 编译器如何识别存在已检查异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10578043/

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