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

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



Back to the top