了解 class E 的抛出表达式

Understanding thrown expression of class E

假设表达式 e 可以抛出 E。表达式是否也可以抛出 E 的任何超类?

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的任何超类型。它可以抛出 E 的超类型的唯一方法是 E 的构造函数抛出一个异常,该异常是 E.

的超类