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

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


Back to the top