[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1062
  • Last Modified:

AspectJ exception handler retrieving object and method information

I'm new to AspectJ and I'm I'm not very familiar with it.  I'm trying to write an exception handler that writes a log whenever an exception is thrown, I need to track which class and method the exception occurred in and the arguments that were passed into that method.  My question is, how would I retrieve the arguments from the method where the exception was thrown?
0
csalem05
Asked:
csalem05
  • 4
  • 3
1 Solution
 
surajgupthaCommented:
If the arguments that were passed to the method is serializable you could serialize it and store the dump of your argument into a log file in the exception's catch block

If you were also looking at how to get the method name, you should use reflection for the same,
0
 
csalem05Author Commented:
I'm not sure what you mean.

If I have this class:

class TestAspect{
    public void testAspect1(String arg1){ throw Exception; }
    public void testAspect2(int arg1, intarg2){  throw Exception(); }
}

And this aspect:

public aspect ExceptionAspect{
   pointcut exceptionHandler(Exception e): handler(Exception+) && args(e);

   before(Exception e): exceptionHandler(e){
       
   }
}

I can get the object where the exception was throw with thisJoinPointStaticPart. I still don't understand how I would get the arguments that were passed into the method that threw the exception.

If I run:
new TestAspect().testAspect1("test");
inside the exceptionHandler aspect how would I get the String "test"
or if I run:
new TestAspect().testAspect2(1, 2);
inside the exceptionHandler aspect how would i get the integers 1 and 2?
0
 
surajgupthaCommented:
> I can get the object where the exception was throw with thisJoinPointStaticPart. I still don't understand how I would get the arguments that were passed into the method that threw the exception.

When an exception is thrown within a function, that exception may be caught, its arguments may be serialized and rethrown as a custom exception message which may be caught in any consuming method and its input parameters analysed.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
surajgupthaCommented:
Method1(Class a, Class b)
try
{
}
Catch
(
string newMessageString = serialize (a) + serialize(b) + use reflection to get the name of the method dynamically;
new exception(newMessageString());
)
0
 
csalem05Author Commented:
I trying to get rid of the try catches, that's why I'm using an aspect.
0
 
surajgupthaCommented:
well how do u think u cud access variables that are passed to some method?? I am not sure if that be done, but again thats just how much i know..
0
 
csalem05Author Commented:
I was hoping there was someway to define a generic set of arguments when defining the pointcut, but I'm beginning to think it's just not possible.

Thanks for your help.
0
 
tarekvcCommented:
check the following tutorial, and check the section named "Exposing Context in Pointcuts"

http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.html
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now