I've seen some code as below in some example BlackBerry Java classes:
try
{
// stuff that will throw an exception
}
catch(final Exception e)
{
// deal with it
}
I presume the final
is for performance. As per the title, since there's rarely (ever?) any reason to modify an Exception
that's already been thrown, should they always be final
?
If so, isn't this something that could be done by the compiler? Or is it done by the compiler and adding the final
manually has no impact at all?
I'm not sure it's about performance, but more about convention. If you're using Eclipse, try to set a formatter that add the
final
keyword wherever it's possible, and reformat your source code with that formatter.I doubt final would really give any performance benefit because the exception instance is block local (Here is a really good answer explaining it https://stackoverflow.com/a/306966/492561).
So It merely serves as a explicit marker that says I will not modify.
Some times you may need to modify the exception to throw it back, may be edit the message to make it more clear at higher levels.
Essentially I would say that its a matter of preference, Some may like it others may not.
I believe
final
is useful when the code which could use it is too long to easily read and understand. e.g. I would make fieldsfinal
where possible to ensure they are assigned correctly in constructors and not modified anywhere in the class.Using
final
for a catch clause is unlikely to help much as a) the value is guaranteed to be set b) the code using it should be short, c) its very rare to modify it anyway.There is nothing stopping you from doing it however.
The Java Language Specification 11.2.2 makes a difference between final and not final exceptions:
Interestingly, JLS 14.20 also says:
In other words, if you don't reassign the
e
of your catch statement (likee = new SomeOtherException();
), it is implicitly declared final.So I can only conclude that it does not make a difference, unless the exception is modified in the catch block and the only example I can come up with is:
I've seen a couple of projects where everything what is not modified must be final (e.g. parameters, fields, local vars etc).
There's also a correspondent style check in PMD code analyzer, which verifies that everything what possible is declared as
final
.