HELP! -- How to find the CURRENT methord name at run time?
Hi,
I am trying to create an common error handler that any code from my project can call to generate messages. I want it to be able to tell the current object's class name and the name of the method where the error occured. Since this handler is shared by all the code, so these names can only be resolved at run time. Could anybody help me on how to do that? I sort of think reflection may potentially be the resolution, but I am not sure.
Please help.
Thank you very much.
Lifeng
[513 byte] By [
125] at [2007-11-15 20:10:11]

# 1 Re: HELP! -- How to find the CURRENT methord name at run time?
Hi,
Since u r going to be writing the Error handler, send a simple "this" pointer to the error handler. Specify the datatype of the parameter as Object. Anything ( objects of any class can now be passed on to this method.. ). In the method, call the object's getClass() method - to get the Class of the object. From then on, u can get hold of the objects methods, fields, etc...
Hmm.. this doesn't give u the method call though...check out something on the Thread stack... - OR - hv the call to the error handler inside the catch{} block. The Exception thrown should contain the information u need. Pass the exception also to your error handler...
Hope that helps.
Rgds,
R. Saravanan
# 2 Re: HELP! -- How to find the CURRENT methord name at run time?
I guess my solution is what you need. I think that you may encapsulate your exception in the class.
In that exception class, it may encapsulate the method name, class name. When the exception is
thrown, it may carry the method name, and the class name. Of course, this method name, and class
name should be implemented by the programmer at that point which occurs error. That point is also
the the point where program throws the exception. This way is similiar with the SQLException. But
the different place is that you put the method , class names instead of SQL error message. This way
might not have to use reflection technique. If using it, it may involve little.
good luck,
Alfred Wu
# 3 Re: HELP! -- How to find the CURRENT methord name at run time?
I faced the same situation when i was asked to write a class to log errors. I ended up passing
method name to the error log function . My error log fuction signature is something like this..
MessageLog.writeException( Object tObject , String strFunctionname , Exception e );
// tObject = this
Getting the class name is real easy. You can use reflection. But from exception object , it's
very difficult to get the function name. you can parse the stack trace , it's kind of difficult and
the format might be changed in the future version. If you find and simple soultion , let me know.
Poochi..
poochi at 2007-11-10 2:55:56 >

# 4 Re: HELP! -- How to find the CURRENT methord name at run time?
Hi Wu,
No, this is not what I want. The only reason I am doing this is because I don't want to pass method names from calling code to the error handler. Instead, I would like to get the method names resolved at RUN TIME. In other words, when a programmer codes his catch() clause from a local method, he simplely call my handler without passing in the method name where he is in, but still the messages printed out by the exception handler, once exception occurs, will include the calling method's name.
Any ideas?
Lifeng
125 at 2007-11-10 2:57:01 >

# 5 Re: HELP! -- How to find the CURRENT methord name at run time?
I understand what you're after, and I'd like to do the same. I think it would be cool for logging trace statements and exceptions. But, like you, I don't want to create additional code.
It seems like the Reflection classes should be able to help, but I've not figured it out either.
Keep asking, this should be possible. I know its easy enough in Smalltalk.
mjpell at 2007-11-10 2:58:03 >
