0

Im am using Spring AOP to log exceptions in my class. It is supposed to log all exceptions thrown in every method of the class.

@AfterThrowing(pointcut = "execution(* *.*(..))", throwing = "e")
protected void method(JoinPoint joinPoint,Exception e) throws Throwable {

However, if the method has a try catch block which catches the exception, advice is not triggered. If i remove the try catch block and add throws clause in method definition then advice is called. Is this how AOP is supposed to work coz it makes no sense? I just want a generic exception handling code for all exceptions at one place instead of in every method.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
user6708151
  • 63
  • 3
  • 12

2 Answers2

4

Yes, this is how it is supposed to work. The term "after throwing" already says when the advice kicks in: after the target method has been throwing an exception. Your method catches and never throws the exception you want to intercept, though. So what can you do?

  • You either have to intercept it one or more levels up the call chain or
  • you upgrade from Spring AOP to AspectJ and use the handler() pointcut in order to intercept the catch block in your method as described in the AspectJ manual. You can also find sample code for handler() in my answer to another question. How to activate full AspectJ with LTW is described in the Spring manual.

As a side note, I am wondering why you are trying to intercept a (hopefully properly) handled exception in the first place. Where is your sample code? What is your use case?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Thanks @kriegaex , i thought spring AOP would be easier to implement than full AspectJ and more relevant as im working on a web application. My use case is that i want to avoid boiler plate logging code. I want to log method entry and exit and exceptions at one place for multiple classes and methods. In exceptions i want to log method name , exception, arguments. – user6708151 Jun 20 '18 at 07:38
  • Method entry exit advices are working fine for my DAO but i dont know how to implement for my servlets as its not a spring application and spring AOP only works for spring injected beans. Exception advices are not working as intended as i cannot throw uncaught exceptions from DAO to servlets. I need to advices to log exceptions in both DAO and servlets. – user6708151 Jun 20 '18 at 07:45
  • Well, my explanation already answers those questions: Use AspectJ instead of spring AOP. AspectJ _can_ be used from withing Spring, but otherwise is completely unrelated and stand-alone, it works with any Java (or other JVM language) application if configured correctly. So I would appreciate you accepting my answer. Thanks. – kriegaex Jun 21 '18 at 02:13
1

If you handle the exception in the method using a try catch, then the method does not throw that exception. AOP is only concerned with the input and output of the method and treats it like a black box, It's not concerned with what goes on inside the method.

Maybe you can move your try catch to the calling method instead.

DaithiG
  • 849
  • 1
  • 9
  • 15