0

Here is Some Code written by some of my Senior team member.

  private void getYourObjectByID(final long someId) {
            TypedQuery<LoanBusinessIndustry> v_objQuery;
            try {
                //Some Code JPA Read Write To Database 
            } catch (final Exception ex) {  //is final here makes any sense ?
                System.err.println("exception get at query getLoanBusinessIndustry=>" + ex.getMessage());
                this.logger.error("No Data found for getLoanBusinessIndustry=>" + ex.toString());
            }
        }

In Parameter of Method getYourObjectByID(final long someId) making Id as a Final make sense. someId Value should not change because Data Objects are fetched from db according to someId But........

 //Complete Catch Block Implemented Code.
catch (final Exception ex) {  //is final here makes any sense ?
       System.err.println("exception get at query =>" + ex.getMessage());
       this.logger.error("No Data found for =>" + ex.toString());
                }

In catch() Block making Exception Class Object ex as final. Is it really make sense because we are handling this exception with in this Catch Block Itself not trying to Rethrow.

Question: Does Final Keyword used in catch make any Sense or not ?????

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52

1 Answers1

2

Adding the final keyword simply makes explicit the fact that the variable is implicitly final. The Java Language Specification states (emphasis my own):

A resource of a try-with-resources statement (§14.20.3) and an exception parameter of a multi-catch clause (§14.20) are implicitly declared final.

Austin
  • 8,018
  • 2
  • 31
  • 37
  • I know that .. Actually i have handled this `exception` in catch block itself okk so ,i just want to know is any purpose of doing that? @austin – Vikrant Kashyap Mar 10 '16 at 10:42
  • 2
    But this isn't a multi-catch clause. Without the `final`, you'd be able to assign `ex = null`. – Andy Turner Mar 10 '16 at 10:49
  • @AndyTurner Thanks! I tried to capture your example in my migrated answer (see link below) @VikrantKashyap My answer applies to *multi-catch caluses*. I moved it to http://stackoverflow.com/a/35915520/3795219 and expanded it to directly address your issue with `final` in a *uni-catch clause*. Let me know when you see this, I'm going to delete this answer. – Austin Mar 10 '16 at 11:47