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?
csalem05Asked:
Who is Participating?
 
surajgupthaConnect With a Mentor Commented:
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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.