Java 7 - 精确地重新抛出最终异常

发布于 2024-11-27 10:22:59 字数 1138 浏览 1 评论 0原文

在以前的java版本中,重新抛出异常被视为抛出catch参数的类型。

例如:

public static void test() throws Exception{
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    try {
        df.parse("x20110731");
        new FileReader("file.txt").read();
    } catch (Exception e) {
        System.out.println("Caught exception: " + e.getMessage());
        throw e;
    }
}

在 Java 7 中,如果声明异常 final,则可以更精确地了解抛出的异常:

//(doesn't compile in Java<7)
public static void test2() throws ParseException, IOException{
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    try {
        df.parse("x20110731");
        new FileReader("file.txt").read();
    } catch (final Exception e) {
        System.out.println("Caught exception: " + e.getMessage());
        throw e;
    }
}

我的问题:文档说我需要声明异常final。但如果我不这样做,上面的代码仍然可以编译并工作。我错过了什么吗?

参考文献:

Project Coin:多次捕获和最终重新抛出
添加更灵活的重新抛出异常检查

In previous versions of java, rethrowing an exception was treated as throwing the type of the catch parameter.

For example:

public static void test() throws Exception{
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    try {
        df.parse("x20110731");
        new FileReader("file.txt").read();
    } catch (Exception e) {
        System.out.println("Caught exception: " + e.getMessage());
        throw e;
    }
}

In Java 7, you can be more precise about the exception being thrown, if you declare the exception final:

//(doesn't compile in Java<7)
public static void test2() throws ParseException, IOException{
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    try {
        df.parse("x20110731");
        new FileReader("file.txt").read();
    } catch (final Exception e) {
        System.out.println("Caught exception: " + e.getMessage());
        throw e;
    }
}

My question: The docs say that I need to declare the Exception final. But if I don't, the code above still compiles and works. Am I missing something?

References:

Project Coin: multi-catch and final rethrow
Add more flexible checking for rethrown exceptions

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰◇生如夏花灿烂 2024-12-04 10:22:59

相信我看到了 Josh Bloch 的一条推文,称“最终”限制已于晚些时候取消。我会看看是否能找到有关它的帖子,但我怀疑这只是您阅读的任何“早期”文档现在都不准确。

编辑:我找不到确切的“已更改”帖子,但 Java 7 文档声明 显示了一个示例,但它不是最终版本。它讨论了当 catch 块声明多个类型时,异常变量隐式为final,但这略有不同。

编辑:我现在找到了困惑的根源,但这是一个内部邮件列表帖子:(无论如何,它不必声明为最终的,但我相信编译器将其视为隐式 最终 - 就像在多重捕获场景中一样。

I believe I saw a tweet from Josh Bloch saying that the "final" restriction had been lifted late on. I'll see if I can find a post about it, but I suspect it's just that any "early" documentation you read is now inaccurate.

EDIT: I can't find the exact "it's changed" post, but the Java 7 documentation states shows an example with it not being final. It talks about exception variables being implicitly final when a catch block declares more than one type, but that's slightly separate.

EDIT: I've now found the source of my confusion, but it's an internal mailing list post :( Anyway, it doesn't have to be declared as final, but I believe the compiler treats it as implicitly final - just like in the multi-catch scenario.

鹿港小镇 2024-12-04 10:22:59

两者都编译的原因是,随后未修改的 uni catch 子句中的异常是隐式最终的(JLS 14.20)。

因此,为了使您的示例无法编译,您需要以某种方式修改 e,例如:

public static void test2() throws ParseException, IOException {
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    try {
        df.parse("x20110731");
        new FileReader("file.txt").read();
    } catch (Exception e) {
        if (e instanceof ParseException) {
            e = new ParseException("Better message", 0);
        } else {
            e = new IOException("Better message");
        }
        System.out.println("Caught exception: " + e.getMessage());
        throw e; //does not compile any more
    }
}

The reason why both compile is that an exception in a uni catch clause that is not subsequently modified is implicitly final (JLS 14.20).

So for your example not to compile, you need to modify e in some way, for example:

public static void test2() throws ParseException, IOException {
    DateFormat df = new SimpleDateFormat("yyyyMMdd");
    try {
        df.parse("x20110731");
        new FileReader("file.txt").read();
    } catch (Exception e) {
        if (e instanceof ParseException) {
            e = new ParseException("Better message", 0);
        } else {
            e = new IOException("Better message");
        }
        System.out.println("Caught exception: " + e.getMessage());
        throw e; //does not compile any more
    }
}
岁月流歌 2024-12-04 10:22:59

没有最后的它仍然是有效的java。你只是失去了它“精确”的好处。

Without the final it is still valid java. You just lose the benefit of it being 'precise'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文