0
votes

We're trying to introduce generic logger in our application using Spring AOP for log statements which are under catch block.

Before AOP

try
{
\\Business Logic 
}
catch(Exception e){
\\some recovery mechanism that won't be generic across different layers
log.error();//These statements needs to be moved to generic logger
}

After going through Spring Docs,I have found this can be done using AfterThrowing advice. After throwing advice is Advice to be executed if a method exits by throwing an exception.

In order to do this We'll to change our existing exception handling code by re throwing Exception inside catch block something like this for AfterThrowing Advice to work.

After AOP:

try
{
\\Business Logic
}
catch(Exception e){
 \\some recovery mechanism that won't be generic across different layers
throw e;
}

AOP code:

@Aspect
@Sl4j
@Component
public class LoggingAdvice {
    @AfterThrowing(pointcut = "execution (* * com..*(..)", throwing = "e")
    public void myAfterThrowing(JoinPoint joinPoint, Exception e) {    
    log.error("Exception occured",e);
    }
}

Do you think is there any better solution than this rather than rethrowing Exception in catch block and propagating it upwards as per call hierarchy?

Note any raised or unchecked exceptions would be catched anyway by AfterThrowing Advice..All i want to do is perform logger clean up by removing log.error inside catch block and have it generic using AOP.

2
Don't catch... What do you gain if you only rethrow?M. Deinum
@M.Deinum I can't..because we have different recovery mechanisms spanned across multiple classes..only reason behind re throwing it is to make AfterThrowing work and catch exception again in generic AOP code to log it.Sagar Kadu
If you only catch and rethrow, there is no sense in doing a catch. It will @AfterThrowing doesn't depend on a catch block.M. Deinum
@M.Deinum agreed @AfterThrowing doesn't depend on a catch block but I want my method to exit with Exception for @AfterThrowing to work so that i can log the stacktrace in AOP code rather than logging it in Business logicSagar Kadu
and how would a catch block with only a throw e when doing a catch (Exception e) help? It is exactly the same if you leave out the catch... Unless you want to convert to a specialized exception and throw that (but that isn't what is clear in the question). Also if you convert why not make the conversion generic and do the logging there instead of using AOP. (M. Deinum

2 Answers

0
votes

As was discussed here, @AfterThrowing is nice for logging exceptions which are actually thrown.

Your case is quite special as you want to log exceptions which are being caught/handled. If you use full AspectJ instead of Spring AOP for this use case you can use a handler(*) pointcut as described with sample code in this answer. It would enable you to factor out your log statements from your catch blocks without the need to escalate (re-throw) exceptions which have already been properly handled, thus changing your logic and making it necessary to catch them somewhere else later.

0
votes

The better approach is to remove the catch blocks as you are going to use @AfterThrowing anyway. And implement whatever you want to implement on top that aspect execution.