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

时间:2012-04-27 07:46:04

标签: exception aspectj

我有一些方法会抛出一些异常,我想使用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不起作用。

3 个答案:

答案 0 :(得分: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);
    }

}

答案 1 :(得分:6)

我担心你不能写出建议来抛出未被声明在匹配的连接点抛出的异常。每个: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个例外的地方。

答案 2 :(得分:2)

有一个"丑陋"解决方法 - 我在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);
    }
}