如何重新抛出异常在AspectJ周围的建议 [英] How to re-throw exception in AspectJ around advise

查看:202
本文介绍了如何重新抛出异常在AspectJ周围的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些方法会抛出一些异常,我想使用AspectJ来计算执行时间,并且抛出一些异常并登录错误日志,并通过重新抛出异常来继续流。 p>

我尝试通过以下方式实现这一点,但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;
}

代码aspectj: -



公共方面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;
抛出新的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不工作。 p>

解决方案

您可以避免捕获异常,只需使用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 =新的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);
}

}


I have some methods which throws some exception, and I want to use AspectJ around advise to calculate the execution time and if some exception is thrown and to log into error log and continue the flow by re-throwing the exception.

I tried to achieve this by following but eclipse says "Unhandled Exception type".

Code-against whom aspectj is to used :-

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

Code aspectj :-

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

Please help why this AspectJ is not working.

解决方案

you can avoid catching the exceptions and just use a try/finally block without the catch. And if you really need to log the exception you can use an after throwing advice, like this:

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

}

这篇关于如何重新抛出异常在AspectJ周围的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆