What do I have
I had set an AspectJ joint point for some specific methods in order to be able to measure their execution time. I never intercept anything in the code flow (so we can call this a "read-only" type of weaved code). The corresponding code looks like:
@Around("execution (* my.package.myclass..*(..)) && @annotation(my.another.package.Monitored)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnObject = null;
long startTime = System.currentTimeMillis();
try {
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
System.out.println("Intercepted exception " + throwable.getClass().getName() + ": " + throwable.getMessage());
throw throwable; //<---- this does the jail-breaking
} finally {
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Monitored annotation = method.getAnnotation(Monitored.class);
//do some more logic as logging etc.
}
return returnObject;
}
Also in the application code itself I have stuff like:
try {
//this is a monitored call:
my.package.myclass.doStuff();
} catch (Throwable anyException) {
//log stuff
//& return default non-null entity
}
Which means I gracefully handle any possible exception at that layer and disallow it to be thrown to the upper layers.
What went wrong
If there is no exceptions thrown by the application code, there are no issues and all the logic works fine - time is measured, logged and tracked. But if there is an exception thrown by the application it escapes the application handler which I posted above and is thrown to the upper layers.
In the debugger I see that it is done by the line which throws throwable
from my aspected handler. And this is something I do not understand. Obviously, if I remove throwing an exception from there it becomes even worse since now the entity will be null
and the whole application flow would be broken
Question
How to properly deal with exception so to log them happened together with conducting all measuring business and do not allow them to jail-break?