当您使用 多catch子句 (的
Exception1 | Exception2e形式
catch),在编译时类型
e是最大的类型两种类型的共同点,因为课程的代码必须处理两种类型exception.From的规范:
异常参数可以将其类型表示为单个类类型或两个或多个类类型的并集(称为替代)。联合的选择在句法上用分隔
|。将异常参数表示为单个类类型 的catch子句 称为 uni-catch子句 。
一个其异常参数表示为类型并集的 catch子句 称为 multi-catch子句 。
…
异常参数的声明类型为,表示其类型为与替代的
D1 | D2 | ... | Dn并集lub(D1, D2, ..., Dn)。
…其中
lub定义的最小上界这里。
如果您想使用特定于
Exception1或的任何内容
Exception2,请使用单独的
catch块:
} catch (Exception1 e) { // Something using features of Exception1} catch (Exception2 e) { // Something using features of Exception2}如果和
info同时存在,则对其进行重构,以使其存在于它们的共同祖先类中:
Exception1``Exception2``info
class TheAncestorException extends Exception { public void info() { // Or possibly make it abstract // ... }}class Exception1 extends TheAncestorException { // Optionally override `info` here}class Exception2 extends TheAncestorException { // Optionally override `info` here}…因此编译器可以指定
e类型
TheAncestorException并使其
info可访问。



