我正在使用 Spring AOP 进行异常处理。 我正在使用周围建议,因为我想记录输入参数。
我的方面方法是这样的:
public Object methodName(ProceedingJointPoint pjp){
Object returnVal = null;
try{
returnVal = pjp.proceed();
} catch(Throable t) {
// Log exception
}
return returnVal ;
}
目前我面临的问题是:当发生异常时我想记录异常但我不想返回null(returnval)。可能吗?
在没有 AOP 的正常情况下:当方法中的某些行在该行之后抛出异常时,不会执行其他行。我想要这样的行为。
我们怎样才能实现它?
最佳答案
良好的旧式检查异常,Java 设计错误一直在困扰着我们。
只需重新抛出可抛出的:
public Object methodName(ProceedingJointPoint pjp) throws Throwable {
...
try {
return pjp.proceed();
} catch (Throwable t) {
// so something with t: log, wrap, return default, ...
log.warn("invocation of " + pjp.getSignature().toLongString() + " failed", t);
// I hate logging and re-raising, but let's do it for the sake of this example
throw t;
}
关于java - 使用 Spring AOP 捕获异常后不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10140086/