Is there ever a reason to not use the final keyword when catching an exception?

后端 未结 5 961
萌比男神i
萌比男神i 2020-12-16 12:03

I\'ve seen some code as below in some example BlackBerry Java classes:

try
{
    // stuff that will throw an exception
}
catch(final Exception e)
{
    // de         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-16 12:04

    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 fields final 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.

提交回复
热议问题