【问题标题】:How to re-throw exception in AspectJ around advise如何在 AspectJ 中围绕建议重新抛出异常
【发布时间】:2012-04-27 07:46:03
【问题描述】:

我有一些引发异常的方法,我想使用 AspectJ 来计算执行时间以及是否引发异常,并登录到错误日志并通过重新引发异常来继续流程。

我试图通过以下方式实现这一点,但 eclipse 说“未处理的异常类型”。

针对谁使用aspectj的代码:-

public interface Iface {
    public void reload() throws TException;

    public TUser getUserFromUserId(int userId, String serverId) throws ResumeNotFoundException, TException;

    public TUser getUserFromUsername(String username, String serverId) throws  ResumeNotFoundException, TException;

    public TResume getPartialActiveProfileFromUserId(int userId, int sectionsBitField, String serverId) throws ResumeNotFoundException, UserNotFoundException;

    public TResume getPartialActiveProfileFromUsername(String username, int sectionsBitField, String serverId) throws ResumeNotFoundException, UserNotFoundException, TException;
}

代码方面:-

public aspect AspectServerLog {

public static final Logger ERR_LOG = LoggerFactory.getLogger("error");

Object around() : call (* com.abc.Iface.* (..)) {
    Object ret;
    Throwable ex = null;

    StopWatch watch = new Slf4JStopWatch();

    try {
    ret = proceed();
    }catch (UserNotFoundException e) {
    ex = e ;
     throw e ;
    } catch (ResumeNotFoundException e) {
    ex = e ;
    throw e ;
    } catch (Throwable e) {
    ex = e ;
    throw new RuntimeException(e);
    }finally{

    watch.stop(thisJoinPoint.toShortString());

    if(ex!=null){
        StringBuilder mesg = new StringBuilder("Exception in ");
        mesg.append(thisJoinPoint.toShortString()).append('(');
        for(Object o : thisJoinPoint.getArgs()) {
        mesg.append(o).append(',');
        }
        mesg.append(')');

        ERR_LOG.error(mesg.toString(), ex);
        numEx++;
    }

   }
return ret;
}
}

请帮助解释为什么这个 AspectJ 不起作用。

【问题讨论】:

    标签: exception aspectj


    【解决方案1】:

    您可以避免捕获异常,而只需使用没有捕获的 try/finally 块。 如果您确实需要记录异常,您可以使用 after throwing 建议,如下所示:

    public aspect AspectServerLog {
    
        public static final Logger ERR_LOG = LoggerFactory.getLogger("error");
    
        Object around() : call (* com.abc.Iface.* (..)) {
    
            StopWatch watch = new Slf4JStopWatch();
    
            try {
                return proceed();
            } finally {
                watch.stop(thisJoinPoint.toShortString());
            }
        }
    
        after() throwing (Exception ex) : call (* com.abc.Iface.* (..)) {
            StringBuilder mesg = new StringBuilder("Exception in ");
            mesg.append(thisJoinPoint.toShortString()).append('(');
            for (Object o : thisJoinPoint.getArgs()) {
                mesg.append(o).append(',');
            }
            mesg.append(')');
    
            ERR_LOG.error(mesg.toString(), ex);
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      恐怕您不能编写建议来抛出未声明为在匹配的连接点抛出的异常。每人:http://www.eclipse.org/aspectj/doc/released/progguide/semantics-advice.html: “通知声明必须包含一个 throws 子句,列出主体可能抛出的已检查异常。此已检查异常列表必须与通知的每个目标连接点兼容,否则编译器会发出错误信号。”

      在 aspectj 邮件列表上已经讨论过如何改善这种情况 - 请参阅这样的主题:http://dev.eclipse.org/mhonarc/lists/aspectj-dev/msg01412.html

      但基本上您需要为异常声明的每个变体提供不同的建议。例如:

      Object around() throws ResumeServiceException, ResumeNotFoundException, TException: 
        call (* Iface.* (..) throws ResumeServiceException, ResumeNotFoundException, TException) {
      

      这将在所有有这 3 个例外的地方提供建议。

      【讨论】:

        【解决方案3】:

        有一个“丑陋”的解决方法 - 我在 Spring4 AbstractTransactionAspect 中找到了它们

        Object around(...): ... {
            try {
                return proceed(...);
            }
            catch (RuntimeException ex) {
                throw ex;
            }
            catch (Error err) {
                throw err;
            }
            catch (Throwable thr) {
                Rethrower.rethrow(thr);
                throw new IllegalStateException("Should never get here", thr);
            }
        }
        
        /**
         * Ugly but safe workaround: We need to be able to propagate checked exceptions,
         * despite AspectJ around advice supporting specifically declared exceptions only.
         */
        private static class Rethrower {
        
            public static void rethrow(final Throwable exception) {
                class CheckedExceptionRethrower<T extends Throwable> {
                    @SuppressWarnings("unchecked")
                    private void rethrow(Throwable exception) throws T {
                        throw (T) exception;
                    }
                }
                new CheckedExceptionRethrower<RuntimeException>().rethrow(exception);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-29
          • 2021-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多