Every time I read the official Spring AOP documentation (link), I get confused by the behavior of advices wrt RuntimeExceptions. Would someone check if my undestanding is correct for the following advices?
@Before
- Advice throws Exception = Target (not executed) Advice (executed)
- Target throws Exception = Target (executed) Advice (executed)
@AfterReturning
- Advice throws Exception = Target (executed) Advice (executed)
- Target throws Exception = Target (executed) Advice (not executed)
@AfterThrowing
- Advice throws Exception = Target (executed) Advice (not executed)
- Target throws Exception = Target (executed) Advice (executed)
@After
- Advice throws Exception = Target (executed) Advice (executed)
- Target throws Exception = Target (executed) Advice (executed)
@Around
- Advice throws Exception = Target (executed) Advice (executed)
- Target throws Exception = Target (executed) Advice (executed up to point.proceed())
This is how I usually look at it.
@Aroundis kind of its own beast. Since you choose when to invoke the target, you are able to catch anyException‘s it maythrow. Therefore, you can easily wrap the call in atry-catch-finallyand have access to any and all of the joinpoints previously mentioned.I am assuming that when you say ‘executed’, you mean ‘executed to the point that the exception was thrown’.
Another thing that may be important to you is the
@Orderannotation. Higher valued@Order‘s occur first on the before side, and they work themselves out backwards on the after side. Make sure you keep that in mind when combining multiple Advices to the same target.