简体   繁体   English

使用Spring AOP时捕获异常后不返回值

[英]Do not return value after catching exception while using Spring AOP

I am using Spring AOP for exception handling. 我使用Spring AOP进行异常处理。 I am using around advice as I want to log the input parameters. 我正在使用周围的建议,因为我想记录输入参数。

My aspect method is like this: 我的方面方法是这样的:

public Object methodName(ProceedingJointPoint pjp){
  Object returnVal = null;
   try{
  returnVal = pjp.proceed();
 } catch(Throable t) {
   // Log exception 

 }
 return returnVal ;

}

The problem currently I am facing is : When the exception is occured I want to log the exception but I do not want to return null (returnval). 我目前面临的问题是:当发生异常时我想记录异常,但我不想返回null(returnval)。 Is it possible? 可能吗?

In the normal scenario without AOP : when some line in the method throws an Exception after that line , no other lines get executed. 在没有AOP的正常情况下:当方法中的某些行在该行之后抛出异常时,不会执行任何其他行。 I want behaviour like that. 我想要这样的行为。

How can we ahieve it? 我们该怎么办呢?

Good old checked exceptions, the Java design mistake that just keeps on biting. 很好的旧的检查异常,Java设计错误,只是继续咬。

Just rethrow the throwable: 只需重新抛出throwable:

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

See here: https://stackoverflow.com/a/5309945/116509 见这里: https//stackoverflow.com/a/5309945/116509

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM