Take a look at section 10.6 from AspectJ in Action (if you don't have the
book, you can download sources from manning.com/laddad2). It users a
thread-local for the same purpose.

-Ramnivas

On Wed, Jun 30, 2010 at 2:04 AM, Ashank <[email protected]> wrote:

>
> Hello All,
>
> I have a simple question regarding 'after throwing' advice. I'm trying to
> advice runtime exceptions/errors originating in my app. My aspect is as
> follows:
>
> aspect ExceptionHandlersAspect extends GenericAspect {
>
>        pointcut AllMethsExecPC():execution(* *(..));
>
>        after() throwing (RuntimeException rte): AllMethsExecPC() {
>                String className =
> thisJoinPointStaticPart.getSignature().getDeclaringTypeName();
>                String methName =
> thisJoinPointStaticPart.getSignature().getName();
>                String methKey = className + "." + methName + "(" +
> AJUtils.getParamNames(thisJoinPointStaticPart) + ")";
>                myLogger.fine("EXCEPTION " + rte + " : In Exception handler
> aspect,
> methkey is " + methKey);
>        }
>
>        after() throwing (Error err): AllMethsExecPC() {
>                String className =
> thisJoinPointStaticPart.getSignature().getDeclaringTypeName();
>                String methName =
> thisJoinPointStaticPart.getSignature().getName();
>                String methKey = className + "." + methName + "(" +
> AJUtils.getParamNames(thisJoinPointStaticPart) + ")";
>                myLogger.fine("ERROR: " + err + " In Exception handler
> aspect, methkey is
> " + methKey);
>        }
> }
>
> Now, these advices detect implicit RuntimeExceptions like
> NullPointerExceptions or Divide by zero ArthimeticExceptions(thrown by the
> JVM) just fine but they capture the same exception everytime it is
> propogated up the call stack!
>
> For e.g.,
>
> int x = 1;
>
>        void catchAndThrowException() {
>                try {
>                        throwException();
>                }
>                catch(RuntimeException e) {
>                        throw e;
>                }
>        }
>
>        void throwException() {
>                if (x==1)
>                        throw new RuntimeException("EGSITE exception");
>        }
>
> In the above code snippet, suppose I am calling catchAndThrowException()
> method.  I just want to detect the runtimeexception in the throwException()
> method and not in the catchAndThrowException() method. Currently, my advice
> matches both these joinpoints.
>
> I just want to capture them when they originate and be done with it.
> Something like !cflowbelow(...). What can I use?
>
> Secondly, my pointcut captures all method executions in my app. Could this
> bog down my application or significantly affect its performance? I cannot
> use within() or withincode() pointcuts although I have excluded some
> packages in my aop-ajc.xml file. Please suggest ideas/optimizations if
> possible! Thank you very much.
>
> -Arvind
>
> --
> View this message in context:
> http://aspectj.2085585.n4.nabble.com/basic-question-regarding-after-throwing-advice-tp2272690p2272690.html
> Sent from the AspectJ - users mailing list archive at Nabble.com.
> _______________________________________________
> aspectj-users mailing list
> [email protected]
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
[email protected]
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Reply via email to