Can we handle exception like try-catch construction with Spring AOP?
When some exception thrown and correspondent aspect contains two advices, with exception class and its superclass,
@AfterThrowing(pointcut = "bean(advisedService)", throwing = "e")
void afterThrowingException(RuntimeException e) {}
@AfterThrowing(pointcut = "bean(advisedService)", throwing = "e")
void afterThrowingException(Exception e) {}
they both are applying.
While in plain catch(…){} we can achieve the desired result by ordering from subclass to superclass (from particular to general):
try {
advisedService.doSomething();
}
catch (RuntimeException e) {}
catch (Exception e) {}
and process handling ex of specific type just once✔️
GitHub //with demo test
The aspect behaves exactly as expected, the way you designed it. Try not to compare apples with pears. In an
@AfterThrowingadvice, you cannot catch/handle exceptions, only do something on top of the exception being thrown, e.g. log them the way you do. Both of your advice methods will fire, because both pointcuts match. So far, so normal.If you want different behaviour, just change the code. How about this?
Or if you really want to handle exceptions and not just log them, use an
@Aroundadvice (made-up example):