2010-12-09 13 views
8

में AsTT में AfterThrowing पर अपवाद कैसे निगलें, मैं एक अपवाद निगलना चाहता हूं।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(); 
} 
} 

ऊपर, यह अपवाद निगल नहीं था। TestThrowable() के कॉलर अभी भी अपवाद प्राप्त किया। मैं कॉलर को अपवाद प्राप्त नहीं करना चाहता हूं। यह कैसे कर सकता है? धन्यवाद।

उत्तर

5

मुझे लगता है कि यह AfterThrowing में नहीं किया जा सकता है। आपको Around का उपयोग करने की आवश्यकता है।

+0

धन्यवाद Tadeusz! मैंने हल किया है! – user389227

5

मेरा समाधान!

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

} 

धन्यवाद।

संबंधित मुद्दे