Abhijit Sarkar
Abhijit Sarkar

Reputation: 24518

Spring AOP: How to determine if method threw an exception using @After?

Spring docs say:

After advice must be prepared to handle both normal and exception return conditions.

@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")
public void doReleaseLock() {
    // ...
}

I'm interested in knowing whether dataAccessOperation completed normally or with an exception. Unfortunately, the above code snippet is in the run for the most useless documentation ever produced. I understand that I can use AfterReturning and AfterThrowing separately, or even Around, but that's not the point. Since After exists, I should be able to use it.

Upvotes: 1

Views: 1847

Answers (1)

kriegaex
kriegaex

Reputation: 67297

What the documentation wants to say is that your @After advice should do a valid thing no matter whether an exception was thrown or not. It does not mean you can determine it from the advice. If different things should be done,

  • either use @AfterReturning vs. @AfterThrowing if the control flow is not to be touched and no exception is to be handled by your advices,
  • or use an @Around advice if you want to actually catch and handle thrown exceptions and/or modify method parameters before call or the return value after the call or skip method execution altogether.

Upvotes: 3

Related Questions