@AfterThrowing is a Spring AOP advice annotation. A method annotated with it runs after a matched method exits by throwing an exception, which makes it a clean place for cross-cutting error handling such as logging, metrics or alerting. It observes the exception but does not catch it: the exception keeps propagating to the caller.
The annotation is org.aspectj.lang.annotation.AfterThrowing. Spring uses AspectJ's annotation style, but applies it with proxy-based AOP at runtime.
In a Spring Boot 3 application, add the AOP starter. It brings in AspectJ's weaver and Spring Boot auto-configures proxy creation, so @EnableAspectJAutoProxy is not needed.
In a plain Spring application, add @EnableAspectJAutoProxy to a configuration class. The aspect itself must be a Spring bean, so annotate it with @Component along with @Aspect.
| Attribute | Meaning |
|---|---|
pointcut (or value) | Pointcut expression or reference to a named pointcut that selects the methods to advise |
throwing | Name of the advice parameter that receives the thrown exception |
The type of the throwing parameter is also a filter. If you declare Exception ex, the advice runs for any Exception; if you declare IllegalStateException ex, it runs only when that type (or a subtype) is thrown. If you omit throwing, the advice runs for every throwable. A JoinPoint can be the first parameter to access the method signature and arguments.
And a service to advise:
Calling paymentService.charge("a-1", 0) from another bean logs the failure from the aspect, and the caller still receives the IllegalArgumentException.
Reusable pointcuts keep aspects readable:
| Advice | Runs when | Can change the outcome |
|---|---|---|
@Before | Before the method | Can throw to stop the call |
@AfterReturning | Method returned normally | Can read, not replace, the result |
@AfterThrowing | Method threw an exception | Cannot swallow or replace the exception |
@After | Always, like finally | No |
@Around | Wraps the call | Yes: can catch, retry, return another value |
If you need to recover from an exception, translate it to another type, or retry, use @Around and call proceed() inside a try-catch. @AfterThrowing is for side effects only.
@AfterThrowing for logging, but avoid double-logging when a global handler such as @RestControllerAdvice also logs the same exception.@Transactional methods, check ordering with @Order if the aspect needs to run inside or outside the transaction.@AfterThrowing stop the exception from propagating?No. The exception continues to the caller after the advice completes. Use @Around if you need to handle it.
Set throwing = "ex" and declare a parameter with the same name, such as Exception ex. Its type also filters which exceptions trigger the advice.
The usual causes are a missing @Component on the aspect, a pointcut that does not match, self-invocation, or the target not being a Spring bean.
Yes. It applies to any exception thrown from the advised method, checked or unchecked, as long as the type matches the throwing parameter.
@AfterThrowing runs advice when a matched Spring bean method throws an exception, which suits logging, auditing and alerting. Use the throwing attribute to receive and filter the exception, remember it cannot swallow the error, and choose @Around when you need to change the outcome.