AOP Exception Handling

后端 未结 4 840
迷失自我
迷失自我 2020-12-15 04:56

I see that Guice and Spring use AOP Alliance under the hood for method interceptions, and I\'ve been trying to figure out how to get AOP Alliance to intercept and handle cer

4条回答
  •  我在风中等你
    2020-12-15 05:48

    You can catch exceptions with Spring AOP, but I do not know if that matches your requirement for a pure Java framework.

    With Spring, you can write a simple AOP interceptor as something like:

    @Aspect
    public class ErrorInterceptor{
    @AfterThrowing(pointcut = "execution(* com.mycompany.package..* (..))", throwing = "ex")
    public void errorInterceptor(WidgetException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Error Message Interceptor started");
        }
    
        // DO SOMETHING HERE WITH EX
        logger.debug( ex.getCause().getMessage());
    
    
        if (logger.isDebugEnabled()) {
            logger.debug("Error Message Interceptor finished.");
        }
    }
    }
    

    but there is no way to return to the calling method or continue processing on the subsequent line. However if you handle the exception here, it won't bubble up the chain unless you rethrow it yourself.

提交回复
热议问题