Renan
Renan

Reputation: 71

Spring AOP - @AfterThrowing

The method below should be invoked when my system throws an exception, but it isn't. It only works when I remove the 'throwing' from the annotation and the 'Exception' as parameter:

Doesn't work:

@AfterThrowing(pointcut="execution(public * br.com.ogfi.*.controller.*.*(..))", throwing="e")
public void afterThrowing(Exception e) {
    System.out.println("Test");
}

Works:

@AfterThrowing(pointcut="execution(public * br.com.ogfi.*.controller.*.*(..))")
public void afterThrowing() {
    System.out.println("Test");
}

Does anyone knows what i'm doing wrong?

This is the entire class:

package br.com.ogfi.portalbackoffice.aspect;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AfterThrowAdvice {


    @AfterThrowing(pointcut="execution(public * br.com.ogfi.*.controller.*.*(..))", throwing="e")
    public void afterThrowing(Exception e) {
        System.out.println("Boo! We want our money back!");
        //ex.printStackTrace();
        //System.out.println("Boo! We want our money back!");

    }

}

Upvotes: 2

Views: 1172

Answers (1)

Renan
Renan

Reputation: 71

Finaly i've found the reason it wasn't working:

The java project i'm working with has his own exception called SystemException, the same name of javax.transaction.SystemException and i haven't realized it wasn't from javax.

The SystemException from my project extends Throwable and when I tried to use Exception as parameter, my method wasn't called because wasn't the same thing.

Upvotes: 2

Related Questions