2

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?

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • 3
    better use **Logging framework** instead of using any *work-around*. – Braj Dec 22 '14 at 11:28
  • you mean to say like log4j. But I have to store the log in dto and handle it somewhere else. – Naman Gala Dec 22 '14 at 11:30
  • How do you define *better*? (Remember, questions that are not objectively answerable but only ask for opinions are not within scope for SO) – Erwin Bolwidt Dec 22 '14 at 12:36
  • I have to include that in code so I am asking.. And just want to clarify that I am going to use it in every catch block... So which one has less overhead? – Naman Gala Dec 22 '14 at 12:44
  • And also i have noticed that 1st solution creates class file... So it was generating many class files. – Naman Gala Dec 22 '14 at 12:55
  • if you are writing same code in block, then use some aspect oriented framework like aspectj and write code at one place instead of calling from everywhere,and there method also give you method name in parameters. – Panther Dec 23 '14 at 05:08
  • Thanks for ur reply @Panther. I am unable understand your point, because of my less knowledge on aspectj. Can you please clarify more. Thanks – Naman Gala Dec 23 '14 at 05:15
  • This supports aspect oriented programming in java. You can have your code in single place by defining that before and after which all methods it can be called. You can look for any tutorial site for same, it is pretty simple and is for same kind of scenario. I use Spring framework so I use Spring AOP, however you can use any aspect oriented framework. – Panther Dec 23 '14 at 05:27

1 Answers1

1

If you are writing same code in after every method like logging entering , existing the method or if you have same kind of exception handling for each method, then you should go fore some aspect oriented framework like aspectj and write code at one place instead of calling from everywhere.

In Aspect you need to define pointcuts and you will get your class name and method name from parameters. You can use simple aspectj framework or Spring AOP if you are using Spring. Apart from that there are other frameworks for aspect oriented coding.

You just need to define some wildcard entries for your methods and classes and your aspect code automatically will be called after method calls.

http://www.javacodegeeks.com/2014/02/applying-aspect-oriented-programming.html

http://www.javatpoint.com/spring-aop-tutorial

Panther
  • 3,312
  • 9
  • 27
  • 50
  • is your user defined message is different for each method ? you can have some mapping in with method name in property file. – Panther Dec 23 '14 at 06:55
  • Yes, It can be different for each method. As I am storing some runtime information like ids or some variable values for debugging purpose. – Naman Gala Dec 23 '14 at 07:02
  • then you have set them in your custom exception in your actual method and you can use aspect only to set classname and method name. but that is not worthy. This handling part of your dto, if it is some common code, then you can do that in aspect. – Panther Dec 23 '14 at 07:06
  • Yes, so I will have to use any one solution from above question only. And I have one more question, if I am handling exception of controller using aspect then how will I `navigate/show error message to user` when exception occurs. – Naman Gala Dec 23 '14 at 07:13
  • Couldn't you use AspectJ to keep a stack in threadlocal that on entry into a method adds the values this person needs, and on exiting a method, pops? the OP could then use your solution and generate their user defined message – Joeblade Dec 23 '14 at 08:53
  • One can use @Around for adding values in parameter or return value. However, in case of exception, no sure if it is possible. – Panther Dec 23 '14 at 08:59
  • @Joeblade my application uses multi-threading, so your solution might create some problem in that case, please correct me if I am wrong. And Panther I referred your links and as you said in case of exception I did not see any extra parameter that I can pass. – Naman Gala Dec 23 '14 at 09:13
  • I have up-voted your answer because it gave me useful concept which I can use in future. – Naman Gala Dec 23 '14 at 09:15
  • 1
    @NamanGala You may want to read up on threadlocal. each thread will have their own instance in threadlocal so this shouldn't be an issue in a multithreaded environment. that's kindof why it exists. – Joeblade Dec 23 '14 at 09:25