Why is @AfterReturning only executed after @AfterThrowing on exception?
I have the following:
@AfterReturning("executionOfTrustedAnnotatedMethod()")
public void afterReturningFromTrustedMethodExecution() { ... }
@AfterThrowing(pointcut = "executionOfTrustedAnnotatedMethod()")
public void afterThrow开发者_如何学CingByExecutionOfTrustedAnnotatedMethod() { ... }
And Im observing this behaviour which does not make sense to me:
- If the method captured by this pointcut does not throw an exception, @AfterReturning is executed
- If the method throws an exception, @AfterReturning is only executed if @AfterThrowing exists and is executed first
What I am trying to accomplish is to run some code at the end of the execution of a method regardless if there was an exception thrown or not. But now this code runs twice (if I have both afterReturning and afterThrowing) or not at all (if I only have afterReturning) if there is an exception thrown.
Any suggestions?
Thanks, Piotr
If you want to execute a code after method regardless if there was an exception thrown or not, you need to use @After
instead.
精彩评论