Skip to main content

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

Ramnivas:

 

Just want to clarify some of the points you brought up.

 

Here is what my customer class looks like. “.

 

Essentially I would like to narrow the join points based on exception type and not catch for e.g all runtime exceptions and have a big switch in my advice.  Here is a sample code I used in my test earlier e.g.

 

Are you weaving into classes throwing the exceptions?” --  Perhaps you could help clarify what you mean by weaving into the class.

 

public class Customer {

      public Customer() {

            System.out.println("customer constructor");

      }

     

      public String getName(String type) throws NullPointerException,

                                                                        BufferOverflowException, MissingResourceException {

            System.out.println("exception requested is of type:" + type);

            if (type.equals("null")) {               

                  throw new NullPointerException("null");

            } else if (type.equals("buffer")) {

                  throw new BufferOverflowException();

            } else if (type.equals("missing")) {

                  throw new MissingResourceException("missing","missing","missing");

            }

           

            return "test";

      }

}

 

With my point cut defined as

ublic aspect ExceptionHandler {

      //pointcuts to specify joinpoints -- collect specific contexts of interest

      pointcut customException():

            (call (* *.*(..) throws NullPointerException)

            || call (* *.*(..) throws BufferOverflowException)

            || call (* *.*(..) throws MissingResourceException))

            && within (HelloWorld);

     

      // advices or actions that we need to take

      after() throwing (RuntimeException ex): customException() {

            System.out.println("In after throwing advice");

            if (ex instanceof MissingResourceException)

                  System.out.println("advice: Found missing resource");

            else if(ex instanceof NullPointerException)

                  System.out.println("advice: Found Null Pointer");

            else if(ex instanceof MissingResourceException)

                  System.out.println("advice: Bufferoverflow");

           

        throw new CustomException(ex);         

    }

}

the number of visible advices when using AJDT 1.2 is confined to the only those that throw these exceptions; however, when I use the pointcut you sent me, every line of my code seems to have a advice attached to it. In eclipse 3.0/ajdt 1.2, I have that advised-by-arrow suddenly show up all over the code.

 

        pointcut customException():
               call (* *.*(..)

                        && within (HelloWorld);

 

 

-

Sudhakar Ramakrishnan

x3520, ESRI

 


From: Ramnivas Laddad [mailto:ramnivas@xxxxxxxxxxxxxxx]
Sent: Tuesday, August 30, 2005 11:53 AM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] Exception Handling PointsCuts/Advices

 

execution() vs. call() shouldn't really matter in you case. Are you weaving into classes throwing the exceptions? If not, that explains why call() works and not execution().

Also, there is a kind of bug in you pointcut: you are specifying runtime exceptions as a part of your pointcuts. Since runtime exceptions specification of methods, if any, is lost in the byte code, you pointcut is effectively the same as the following:

        pointcut customException():
               call (* *.*(..)
               && within (HelloWorld);        

This bug, however, is unrelated to execution() vs. call() issue.

-Ramnivas

===
Ramnivas Laddad,
Author, AspectJ in Action
http://ramnivas.com
M: (408)203-4621



Sudhakar Ramakrishnan wrote:

If I use execution pointcuts, then the exception gets throw in the code
and it never gets caught in the after throwing(). So I repaced them with
the call point cut. Is this correct? 
 
public aspect ExceptionHandler {
        //pointcuts to specify joinpoints -- collect specific contexts
of interest
        pointcut customException():
               (call (* *.*(..) throws NullPointerException)
               || call (* *.*(..) throws BufferOverflowException)
               || call (* *.*(..) throws MissingResourceException))
               && within (HelloWorld);        
        
        // advices or actions that we need to take
        after() throwing (RuntimeException ex): customException() {  
               System.out.println("In after throwing advice");
               if (ex instanceof MissingResourceException)
                       System.out.println("advice: Found missing
resource");
               else if(ex instanceof NullPointerException)
                       System.out.println("advice: Found Null
Pointer");
               else if(ex instanceof MissingResourceException)
                       System.out.println("advice: Bufferoverflow");
               
        throw new CustomException(ex);        
    }
}
-
Sudhakar Ramakrishnan
x3520, ESRI 
 
 
-----Original Message-----
From: Ramnivas Laddad [mailto:ramnivas@xxxxxxxxxxxxxxx] 
Sent: Tuesday, August 30, 2005 11:21 AM
To: aspectj-users@xxxxxxxxxxx
Subject: Re: [aspectj-users] Exception Handling PointsCuts/Advices
 
You are almost there. Try the following advice instead:
 
after() throwing(RuntimeException ex): customException() {      
    // use ex...
    // do some processing with the exception object, like logging etc
    throw new CustomException(ex);    // chaining 'ex' is a good idea
}
 
-Ramnivas
 
===
Ramnivas Laddad,
Author, AspectJ in Action
http://ramnivas.com
M: (408)203-4621
 
 
 
Sudhakar Ramakrishnan wrote:
 
  
How would one go about define pointcuts and advices for something like
this?
 
Class A throws a number of different types of unchecked exceptions. I
want to define a pointcut that captures these execution join points,
catch the unchecked exception, do some processing with the exception
object, and possibly rethrow the exception as a custom exception. If
anyone has samples for this it would be great.
 
More details:
 
Let's say we have
Class A:
public void createAccount(String type) {
  if (type.equals("aaa")) {                     
         throw new NullPointerException("aaa");
  } else if (type.equals("bbb")) {
         throw new BufferOverflowException();
  } else if (type.equals("cccc")) {
         throw new
MissingResourceException("missing","missing","missing");
  }
}        
 
We create Class A objects in Class B.
Class B:
 
A obj = new A();
Try {
A.createAccount();
} catch (CustomException e) {
  // hoping aspectj has rethrow certain unchecked exceptions as
customexceptions
}
 
Essentially, I want to capture some of those unchecked exceptions and
cast it into a customexception. If there are checked I will convert
    
them
  
to soft exceptions and convert them if needed.
 
 
I am starting with something like this and want some help since I am
    
new
  
to AspectJ syntax.
 
public aspect ExceptionHandler {
  pointcut customException():
         (execution (* *.*(..) throws NullPointerException)
         || execution (* *.*(..) throws BufferOverflowException)
         || execution (* *.*(..) throws
MissingResourceException))
         && within (A); 
  
  // I guess I need after throwing
   after() : customException() {           
       //do some processing with the exception object, like logging
    
etc
  
       throw new CustomException();     
   }
}
-
Sudhakar Ramakrishnan
 
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users
 
 
 
    
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users
 
  

Back to the top