getMethodName()

Discussion in 'Java' started by google@aikempshall.freeserve.co.uk, Jan 25, 2006.

  1. Guest

    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);

    }
     
    , Jan 25, 2006
    #1
    1. Advertising

  2. On 25 Jan 2006 06:34:53 -0800, wrote:
    > 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

    --
    [ 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
     
    Gordon Beaton, Jan 25, 2006
    #2
    1. Advertising

  3. wrote:
    > 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

    --
    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/
     
    Thomas Weidenfeller, Jan 25, 2006
    #3
  4. Guest

    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
     
    , Jan 27, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page