aspose file tools*
The moose likes Aspect Oriented Programming and the fly likes AspectJ: catch and handle exception using aspectJ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Aspect Oriented Programming
Bookmark "AspectJ: catch and handle exception using aspectJ" Watch "AspectJ: catch and handle exception using aspectJ" New topic
Author

AspectJ: catch and handle exception using aspectJ

Rajan Dhabalia
Greenhorn

Joined: Nov 22, 2011
Posts: 2
Hi,

I am having issue regarding Aspectj exception handling. I have following aspectJ advice which is called when any throwable exception is thrown from the java code execution.
Advice:
after() throwing (Throwable t): execution(public * *(..)) && within (com.test.TestClass) {
String method = thisJoinPoint.getSignature().getName();
System.err.println("Threw in the method :"+t.getMessage()+". "+method);
calTrans.setStatus(t);
}
Above advice will print the exception when any exception is thrown from public method of TestClass. Hence, following code is working for the following public method of TestClass:
1. Explicit Exception handling

public void callMethod() {
int a =1/0;
}

But when method has try and catch block inside it and method is not throwing exception explicitly at that time above advice will not work. So, following code is not taken care by above AspectJ advice.

2. Inside Exception:

public void callMethod() {

try{
int a =1/0;
}catch(Throwable th){
System.out.println("I am at catch ");
}
}


So, can anybody please suggest AspectJ advice which will be called when any method has try and catch block. and when that catch_block catches the exception , it should call AspectJ advice.

Thanks in advance.

Thanks,
Rajan
Emil Jennings
Ranch Hand

Joined: Jul 09, 2010
Posts: 48
Rajan,

The after() throwing() executes after the failed execution of the join point. Your join point is the execution of public void callMethod() and the advice is only applied after this join point fails. By adding the try/catch block the join point handles the exception and does not fail. If you want callMethod() to handle the exception and still have the advice applied to the join point then you might want to have the try/catch block throw an exception, like:



You may want to add a try/catch block to whatever calls callMethod() to handle the exception callMethod() is throwing.
Rajan Dhabalia
Greenhorn

Joined: Nov 22, 2011
Posts: 2
Thanks Emil. However, my concern was slightly different and I got what I needed so, I want to share it.

I wanted to create a plugin which can monitor all public methods of a given class. This public method returns Response with failure or success acknowledgement.

If runtime exception comes and if app is not handling it or catching it properly then caller of above public methods will not receive any response or acknowledgement because application will get terminated abruptly.

In order to rid of with this issue, we can create plugin with aspectJ that can
a. log before starting of any method_execution
b. Catch RuntimeException or CheckedException -> Log it
c. Set the Response object with Failure and error_code.
d. Returns Response object rather abnormally terminating application without any response.

I am sharing following solution which can be helpful to design handler for a complex system which lacks adaptability and manageability.

i.e.: We have Engine.Java class which has public methods. aspectJ is used for Logging and errorHandling:


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AspectJ: catch and handle exception using aspectJ