gpt4 book ai didi

java - 了解 E 类抛出的表达式

转载 作者:行者123 更新时间:2023-11-30 11:07:42 26 4
gpt4 key购买 nike

假设表达式e 可以抛出E。表达式是否也可以抛出 E 的任何父类(super class)?

JLS Exception Descriptions没有提到它。

A throw statement (§14.18) whose thrown expression has static type E and is not a final or effectively final exception parameter can throw E or any exception class that the thrown expression can throw.

你不能解释一下吗?

最佳答案

基于 JLS 引用 + 后面的示例:

For example, the statement throw new java.io.FileNotFoundException(); can throw java.io.FileNotFoundException only. Formally, it is not the case that it "can throw" a subclass or superclass of java.io.FileNotFoundException.

如果写throw new E(),这个表达式只能抛出E或者E的构造函数抛出的异常。

因此,答案是否定的,表达式不能抛出E的任何父类(super class)型。它可以抛出 E 父类(super class)型的唯一方法是 E 的构造函数抛出一个 E 父类(super class)型的异常。

关于java - 了解 E 类抛出的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28791251/

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