sajeyasok
sajeyasok

Reputation: 21

How to catch the exception thrown from a Spring AOP Advice

I have my own exception "MyOwnException" and throw this exception from my service class

public void service() throws MyOwnException
{
// some code 

}

Now I want to catch MyOwnException in an advice and rethrow a brand-new Exception

public class SimpleThrowsAdvice implements ThrowsAdvice {

    public void afterThrowing(Method method, Object[] args, Object target,
                MyOwnException ex) throws Throwable {
        throw new Exception("new Description",ex);
    }
}

Now, how can I catch the re-thrown Exception from the above Advice SimpleThrowsAdvice?

Upvotes: 2

Views: 7671

Answers (1)

Behrang Saeedzadeh
Behrang Saeedzadeh

Reputation: 47913

You should use the Around advice to do that. See here Spring AOP AfterThrowing vs. Around Advice

Upvotes: 4

Related Questions