如何在AspectJ中重新抛出异常建议

Arp*_*tal 7 aspectj exception

我有一些方法会抛出一些异常,我想使用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;
}
Run Code Online (Sandbox Code Playgroud)

代码方面j: -

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;
}
}
Run Code Online (Sandbox Code Playgroud)

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

anj*_*osc 12

你可以避免捕获异常,只使用没有catch的try/finally块.如果你真的需要记录异常,你可以使用抛出后的建议,如下所示:

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

}
Run Code Online (Sandbox Code Playgroud)


小智 6

我担心你不能写出建议来抛出未被声明在匹配的连接点抛出的异常.Per: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) {
Run Code Online (Sandbox Code Playgroud)

这将建议具有这3个例外的所有地方.