处理特定的异常类型

时间:2016-11-17 13:57:41

标签: aspectj

我已经定义了两个AfterThrowing建议来处理具有相同切入点的异常。

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


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

如果异常是GatewayException,有没有办法阻止执行泛型方法1?

任何想法都非常感激

C

1 个答案:

答案 0 :(得分:0)

最简单的方法是检查建议体内的异常实例,如果是更具体的异常类型,则提前返回:

@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
}

我知道您期望基于某些AspectJ语言构造的解决方案,但问题是,没有这样的构造。

相关问题