I created a @Before Advice that throws an exception and tried to catch it in another @AfterThrowing, but it does not work.
If the exception is not thrown in the advice, but directly in the method, it works.
If it is thrown in the advice, the @AfterThrowing is not executed.
Why does it behave like that?
Because
@AfterThrowingmeans: catch the event that an exception is thrown from within the captured method. But@Beforemeans: do something before (i.e. outside) the captured method. So your control flow never reaches a point where it could satisfy the condition for the@AfterThrowingadvice.So either you do what Frank told you (use an
@Aroundadvice) or, if you have control over the caller as well as the callee part of the code, you could do this (quite ugly):@Before("execution(myMethod)")(possibly throwing an exception)AfterThrowing("call(myMethod)")I have not tested it, but it should work because of this control flow:
I.e. “before execution” is already “within call”.