I am trying to log method name in exception catch block
.
I have found following solutions from Getting the name of the current executing method
String name = new Object(){}.getClass().getEnclosingMethod().getName();
or
String name = Thread.currentThread().getStackTrace()[1].getMethodName();
or
String name = new Exception("is not thrown").getStackTrace()[0].getMethodName();
Which option is better to get method name in catch block?
Note: I want to use the code in every catch block of my application, So I am asking for the solution which has less overhead.
Thanks.
Update:
Following steps I am doing to handle the exception
1. Catching the exception in try catch block
2. Wrapping that exception in MyException.
3. Setting method name, class name and user defined message. (For this step I have asked this question)
4. Throwing back the MyException.
5. Finally handling MyException in Controller.
Using Spring AOP I can achieve above scenario. But how will I set user defined message
in MyException?