• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AspectJ: catch and handle exception using aspectJ

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 75
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


 
reply
    Bookmark Topic Watch Topic
  • New Topic