3 After throwing advice
After throwing advice runs when a matched method execution exits by throwing an
exception. It is declared using the @AfterThrowing annotation:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;
@Aspect
public class AfterThrowingExample {
@AfterThrowing(pointcut="com.xyz.myapp.service()",throwing="ex")
public void logRequiredTask(DataAccessException ex) {
// ...
}}
The name used in the throwing attribute must correspond to the name of a parameter in the advice method. When a method execution exits by throwing an exception, the exception will be passed to the advice method as the corresponding argument value. A throwing clause also restricts matching to only those method executions that throw an exception of the specified type.