2010-12-09 28 views
8

Dans AspectJ, je veux avaler une exception.Comment avaler une exception à AfterThrowing dans AspectJ

@Aspect 
public class TestAspect { 

@Pointcut("execution(public * *Throwable(..))") 
void throwableMethod() {} 

@AfterThrowing(pointcut = "throwableMethod()", throwing = "e") 
public void swallowThrowable(Throwable e) throws Exception { 
    logger.debug(e.toString()); 
} 
} 

public class TestClass { 

public void testThrowable() { 
    throw new Exception(); 
} 
} 

Au-dessus, il n'a pas avalé l'exception. L'appelant de testThrowable() a toujours reçu l'exception. Je veux que l'appelant ne reçoive pas d'exception. Comment peut-on faire ça? Merci.

Répondre

5

Je pense que cela ne peut pas être fait en AfterThrowing. Vous devez utiliser Around.

+0

Merci à Tadeusz! J'ai résolu! – user389227

5

Ma solution!

@Aspect 
public class TestAspect { 

    Logger logger = LoggerFactory.getLogger(getClass()); 

    @Pointcut("execution(public * *Throwable(..))") 
    void throwableMethod() {} 

    @Around("throwableMethod()") 
    public void swallowThrowing(ProceedingJoinPoint pjp) { 
     try { 
      pjp.proceed(); 
     } catch (Throwable e) { 
      logger.debug("swallow " + e.toString()); 
     } 
    } 

} 

Merci encore.