Cathal
Cathal

Reputation: 1790

handling a specific exception type

I've defined two AfterThrowing advices to handle exceptions with the same pointcut.

@AfterThrowing(pointcut="...", throwing="ex")
public void method1(Exception ex) {}


@AfterThrowing(pointcut="...", throwing="ex")
public void method2(GatewayException ex) {}

Is there a way for me to prevent the generic method1 being executed if the exception is a GatewayException?

Any ideas greatly appreciated

C

Upvotes: 0

Views: 251

Answers (1)

Nándor Előd Fekete
Nándor Előd Fekete

Reputation: 7098

It would be the easiest to check the instance of the exception inside the advice body and return early if it's of the more specific exception type:

@AfterThrowing(pointcut="...", throwing="ex")
public void method1(Exception ex) {
    if (ex instanceof GatewayException) {
        return;
    }
    // handle the more generic Exception case
}


@AfterThrowing(pointcut="...", throwing="ex")
public void method2(GatewayException ex) {
    // handle the more specific GatewayException
}

I know you expected a solution based on some AspectJ language construct, but the thing is, there's no such construct.

Upvotes: 1

Related Questions