Java catch块,捕获的异常不是final [英] Java catch block, caught exception is not final

查看:166
本文介绍了Java catch块,捕获的异常不是final的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html



关于捕捉多重功能,当我遇到这个声明:


注意:如果一个catch块处理多个异常类型,那么
catch参数是隐含的final。在这个例子中,catch
参数ex是final,因此你不能在catch块中为
指定任何值。


我从来没有注意到,在处理捕获的异常的经典案例中,捕获到的异常不是最终的。



我只是想知道为什么这是一件好事第一名?在我猜想重新抛出它或者可能记录它的消息之前,是否不会错误地修改捕获的异常?是否应该由触发机制创建异常,因此它应该代表什么?



我从未看到在catch块中修改的异常可能有人指出它的好处?



谢谢!

解决方案

方法参数大致相同:



您通常不修改它们,而许多人们同意他们应该被视为 final (无论是否在其前面实际写入 final 是一个有争议的问题)。



但是由于没有技术要求,它必须是 final ,该语言可以让您选择。



个人而言,我知道没有任何理由修改catch块的异常引用。


I am checking out the new features of Java SE7 and I am currently at this point:

http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html

regarding the catch multiple feature, when I came across this statement:

Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.

I never noticed that the caught exception is not final in the classic case of handleing caught exceptions.

I just wonder why is that a good thing in the first place? Would it not be ill-advised to essetially MODIFY a caught exception before I guess rethrowing it or maybe logging it's message? Should it not be up to the trowing mechanism to create the exception so it represents exactly what it should?

I have never seen an exception being modified in the catch block can maybe someone point out it's benefits?

Thanks!

解决方案

It's pretty much the same as method arguments:

You usually don't modify them and many people agree that they should be treated as final (whether or not to actually write final in front of them is a matter of some debate).

But since there's no technical requirement that says it must be final, the language gives you the option to choose.

Personally I know of no good reason to modify the exception reference of a catch-block.

这篇关于Java catch块,捕获的异常不是final的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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