Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Exception Handling

Hi Tim,

It is certainly possible to do this with AspectJ, although the real question
in my mind is:
1) What counts as eating the exception? Doing or calling nothing? Not
setting a value?
2) I also think there are cases where code legitimately eats an exception,
e.g., in test cases that are testing for failure. I've also seen this in
valid code that had empty catch blocks when it was detecting, e.g.,
ClassNotFoundException or NumberFormatException. Of course with this policy
in place, the latter case could be refactored to act within the catch block
instead of using an if (var==null) afterwards... 

I think something like this would meet your needs (although I haven't
compiled or tested it). However, it will probably add significant overhead
to the underlying code - at least it adds runtime checks on every method
execution and call. So I might use this for testing but probably not for
production code:

aspect DetectSwallowedExceptions {
    ThreadLocal<Throwable>> handling = new ThreadLocal<Throwable>();

    before(Throwable t) : handler(*) && args(t) && scope() {
        handing.put(t);
    }

    before() : actionPoint() && scope() && if (handling.get() != null) {
         handling.set(null);
    }

    after() returning: enforcementPoint() && if (handling.get() != null) &&
scope() {
        throw new ExceptionPolicyFailure("Ate exception in "+
          thisJoinPointStaticPart.getSignature().getName(), handling.get());
    }
   
    protected pointcut enforcementPoint() : 
        execution(* *(..)) || execution(new(..)) || adviceexecution() || 
        initialization(new(..)) || staticinitialization(*);

    // join points that count as acting, i.e., making a call or setting a
value (or running advice?)
    protected pointcut actionPoint() : (call(* *(..)) || call(new(..)) ||
set(* *(..))); /* || advicexecution() */

    protected pointcut scope() : within(com.myapp..*) /*&&
!within(TestCase+)*/ && !within(DetectSwallowedExceptions);
}

public exception ExceptionPolicyFailure extends RuntimeException...

________________________________________
From: aspectj-users-bounces@xxxxxxxxxxx
[mailto:aspectj-users-bounces@xxxxxxxxxxx] On Behalf Of Tim McNamara
Sent: Friday, January 20, 2006 6:08 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] Exception Handling

Is it possible to advice methods that "eat" exceptions so that they always
throw a runtime exception.  For example:

public void someMethod() {
   try {
      someMethodThrowsCheckedException();
   } 
   catch(Exception e) { /* do absolutely nothing */ }


I would like to advise this method, without modifying the siganture, to
throw an unchecked exception when "Exception" is thrown

Thanks in advance 
Tim McNamara
      



Back to the top