Java 7 - 具有最终异常的精确推翻 [英] Java 7 - Precise rethrow with a final Exception

查看:154
本文介绍了Java 7 - 具有最终异常的精确推翻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在以前的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(捕获异常:+ e.getMessage());
throw e;
}
}

在Java 7中,您可以更精确地抛出异常,如果您声明异常 final

  // (不在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;
}
}

我的问题: docs说我需要声明Exception final 。但是如果我没有,上面的代码仍然编译和工作。我错过了什么吗?



参考文献:



项目硬币:多次捕获和最终重新投放

添加更灵活的检查重新引导的异常

解决方案

相信 Josh Bloch的鸣叫说最后的限制已经在晚些时候被解除了。我会看看我是否可以找到一篇关于它的帖子,但我怀疑这只是你阅读的任何早期文档现在都不准确。



编辑:我可以'找到确切的它已更改的帖子,但 Java 7文档状态显示了一个例子,它不是是最终的。当catch块声明多个类型,但是稍微分开时,它会将异常变量隐式为final。



编辑:我已经现在发现我的混乱的根源,但它是一个内部的邮件列表发布:(无论如何,它不必被声明为final,但我相信编译器将其视为隐式 final - 就像在多捕获场景中。


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

解决方案

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.

这篇关于Java 7 - 具有最终异常的精确推翻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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