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("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
。但如果我不这样做,上面的代码仍然可以编译并工作。我错过了什么吗?
参考文献:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信我看到了 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.
两者都编译的原因是,随后未修改的 uni catch 子句中的异常是隐式最终的(JLS 14.20)。
因此,为了使您的示例无法编译,您需要以某种方式修改 e,例如:
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:
没有最后的它仍然是有效的java。你只是失去了它“精确”的好处。
Without the final it is still valid java. You just lose the benefit of it being 'precise'.