| Author |
AspectJ: catch and handle exception using aspectJ
|
Rajan Dhabalia
Greenhorn
Joined: Nov 22, 2011
Posts: 1
|
|
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: 33
|
|
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.
|
 |
 |
|
|
subject: AspectJ: catch and handle exception using aspectJ
|
|
|