2016-11-17 59 views
0

我已经定义了两个AfterThrowing通知来处理具有相同切入点的异常。处理特定的异常类型

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


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

如果异常是一个GatewayException,我有办法阻止泛型方法1被执行吗?

任何想法不胜感激

Ç

回答

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语言构造的解决方案,但事情是,没有这样的构造。

+0

是的,我希望避免instanceof,但我认为你是对的。 – Cathal