简体   繁体   中英

Do not return value after catching exception while using Spring AOP

I am using Spring AOP for exception handling. 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). 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. I want behaviour like that.

How can we ahieve it?

Good old checked exceptions, the Java design mistake that just keeps on biting.

Just rethrow the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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