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);
}
Gordon Beaton - 25 Jan 2006 14:39 GMT
> 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

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Thomas Weidenfeller - 25 Jan 2006 14:42 GMT
> 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

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
google@aikempshall.freeserve.co.uk - 27 Jan 2006 14:55 GMT
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