getMethodName()

G

google

I have the following code in a subroutine of a class which is intended
to give the user (me) some information as to the nature of the error.
In addition I want to include in the MessageDialog the name of the
method that invoked the error. How do I get hold of the method name
when running?
See below especially ??????.getMethodName. Am I close?

try {


} catch (ParserConfigurationException pce) {

JOptionPane.showMessageDialog(
null,
"explicit error message",
??????.getMethodName() + " ParserConfigurationException - Error!",
JOptionPane.ERROR_MESSAGE);

}
 
G

Gordon Beaton

I have the following code in a subroutine of a class which is
intended to give the user (me) some information as to the nature of
the error. In addition I want to include in the MessageDialog the
name of the method that invoked the error.
How do I get hold of the method name when running?
[...]

} catch (ParserConfigurationException pce) {

JOptionPane.showMessageDialog(
null,
"explicit error message",
??????.getMethodName() + " ParserConfigurationException - Error!",


Read the documentation for java.lang.Throwable. This is what you need:

pce.getStackTrace()[0].getMethodName();

/gordon
 
T

Thomas Weidenfeller

I have the following code in a subroutine of a class which is intended
to give the user (me) some information as to the nature of the error.
In addition I want to include in the MessageDialog the name of the
method that invoked the error. How do I get hold of the method name
when running?

Start with Throwable.getStackTrace(). The rest is apparent from the API
documentation of that method. I am to lazy to look it up at the moment.

/Thomas
 
G

google

Interrogating the getStackTrace only got the failed method in the java
libraries. What I wanted was the failed method in the code that I'd
written. However, the above suggestion did point me in the right
direction. So what I did to get the method was to loop through each of
elements of the stack trace until I found a match on my class. I Then
found
fillInStackTrace that put my Method to the top of the stack so then I
could use the above
pce.getStackTrace()[0].getMethodName();

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top