Skip to content
Advertisement

Java catch block, caught exception is not final

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 essentially 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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement