2010-12-09 9 views
8

In AspectJ möchte ich eine Ausnahme verschlucken.Wie schlucke ich eine Ausnahme bei AfterThrowing in 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(); 
} 
} 

Oben hat es Ausnahme nicht geschluckt. Der Aufrufer von testThrowable() hat weiterhin die Ausnahme erhalten. Ich möchte, dass der Anrufer keine Ausnahme erhält. Wie kann das gemacht werden? Danke.

Antwort

5

Ich denke, dass es in AfterThrowing nicht getan werden kann. Sie müssen Around verwenden.

+0

Danke Tadeusz! Ich habe gelöst! – user389227

5

Meine Lösung!

@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()); 
     } 
    } 

} 

Nochmals vielen Dank.

Verwandte Themen