多捕获块中的引用类型的异常问题 [英] reference type of exception issue in multi-catch block

查看:119
本文介绍了多捕获块中的引用类型的异常问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是Exception类型但在下面的代码中输出Exception1:

e is of type Exception but prints Exception1 in below code:

class Exception1 extends IOException {void info(){}}
class Exception2 extends Exception {}

class TestMultiCatch {
    public static void main(String args[]) {
        try {
            int a = 10;
            if (a <= 10)
                throw new Exception1();
            else
                throw new Exception2(); 
        } catch (Exception1 | Exception2 e) {
            e.info();  //line 1 error "The method info() is undefined for type Exception"
            System.out.println(e);   //prints Exception1 (after commenting line 1)
        }
    }
}

从我研究的e应该是Exception类型,它是Exception1和Exception2的通用基类。从第1行的消息可以看出它是什么。

From what I studied "e" should be of type Exception which is common base class of Exception1 and Exception2. Which it is, as evident from message in line 1.

但是为什么:

System.out.println(e); //prints Exception1 and not Exception
System.out.println(e instanceof IOException); //prints true and not false
System.out.println(e instanceof Exception1); //prints true and not false
System.out.println(e instanceof Exception2); //false


谢谢。

推荐答案

当你使用 multi-catch子句时( Exception1 | Exception2 e catch 的形式),编译时类型 e 是两种类型共有的最大类型,因为代码必须处理任何类型的异常。来自规范

When you use a multi-catch clause (the Exception1 | Exception2 e form of catch), the compile-time type of e is the greatest type that the two types have in common, since of course the code has to handle either type of exception.From the spec:


异常参数可以将其类型表示为单个类类型或两个或更多类类型的联合(称为替代)。联合的替代方法在语法上由 | 分隔。

一个catch子句,其异常参数表示为单个类类型称为 uni-catch子句

A catch clause whose exception parameter is denoted as a single class type is called a uni-catch clause.

一个catch子句,其exception参数表示为类型的并集,称为 multi-catch子句

A catch clause whose exception parameter is denoted as a union of types is called a multi-catch clause.

...

异常参数的声明类型表示其类型为与替代 D1 |的联合D2 | ...... | Dn lub(D1,D2,...,Dn)

The declared type of an exception parameter that denotes its type as a union with alternatives D1 | D2 | ... | Dn is lub(D1, D2, ..., Dn).

...其中 lub 是定义的最小上限这里

...where lub is Least Upper Bound as defined here.

如果你想要使用特定于 Exception1 Exception2 的任何内容,使用单独的 catch 块:

If you want to use anything that's specific to Exception1 or Exception2, use separate catch blocks:

} catch (Exception1 e) {
    // Something using features of Exception1
} catch (Exception2 e) {
    // Something using features of Exception2
}

如果 info 同时存在 Exception1 Exception2 ,重构它们,以便 info 存在于它们的共同祖先类中:

If info exists on both Exception1 and Exception2, refactor them so that info exists on a common ancestor class of them:

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 可访问。

...so the compiler can give e the type TheAncestorException and make info accessible.

这篇关于多捕获块中的引用类型的异常问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆