How to display the stacktrace?

Discussion in 'Java' started by Aaron Fude, Apr 2, 2008.

  1. Aaron Fude

    Aaron Fude Guest

    Hi,

    Very often, for debugging purposes in Swing applications, I like to
    see the stacktrace so I do the following:

    try {
    throw new RuntimeException();
    }
    catch (Exception e) {
    e.printStackTrace();
    }

    Is there a more tasteful way of accomplishing this?

    Thanks!
     
    Aaron Fude, Apr 2, 2008
    #1
    1. Advertising

  2. Aaron Fude

    Mark Space Guest

    Aaron Fude wrote:
    > Hi,
    >
    > Very often, for debugging purposes in Swing applications, I like to
    > see the stacktrace so I do the following:
    >
    > try {
    > throw new RuntimeException();
    > }
    > catch (Exception e) {
    > e.printStackTrace();
    > }
    >


    I think just this will work:

    Exception e = new Exception();
    e.printStackTrace();

    or even

    new Excpetion().printStackTrace();

    No need to throw it first.
     
    Mark Space, Apr 2, 2008
    #2
    1. Advertising

  3. Aaron Fude

    Aaron Fude Guest

    On Apr 1, 10:44 pm, Mark Space <> wrote:
    > Aaron Fude wrote:
    > > Hi,

    >
    > > Very often, for debugging purposes in Swing applications, I like to
    > > see the stacktrace so I do the following:

    >
    > >           try {
    > >             throw new RuntimeException();
    > >           }
    > >           catch (Exception e) {
    > >             e.printStackTrace();
    > >           }

    >
    > I think just this will work:
    >
    >    Exception e = new Exception();
    >    e.printStackTrace();
    >
    > or even
    >
    >    new Excpetion().printStackTrace();
    >
    > No need to throw it first.


    Cool beans!
     
    Aaron Fude, Apr 2, 2008
    #3
  4. Aaron Fude

    Logan Shaw Guest

    Aaron Fude wrote:
    > Very often, for debugging purposes in Swing applications, I like to
    > see the stacktrace so I do the following:
    >
    > try {
    > throw new RuntimeException();
    > }
    > catch (Exception e) {
    > e.printStackTrace();
    > }
    >
    > Is there a more tasteful way of accomplishing this?


    How about Thread.dumpStack()?

    Or if you want control over where it goes, then you can get
    an array from Thread.currentThread().getStackTrace() and
    print each element of that, or make it available in your GUI
    somewhere if that's appropriate/convenient.

    - Logan
     
    Logan Shaw, Apr 2, 2008
    #4
  5. Aaron Fude

    Francois Guest

    "Aaron Fude" <> a écrit dans le message de news:
    ...
    > Very often, for debugging purposes in Swing applications, I like to
    > see the stacktrace so I do the following:


    You can also do it manually with StackTraceElement :

    catch(Exception e)
    {
    StackTraceElement[] s=e.getStackTrace();
    int n;
    for (n=0; n<s.length; n++)
    {
    g.drawString(s[n].toString(), 0, n*20);
    }
    }

    Francois
     
    Francois, Apr 2, 2008
    #5
  6. Aaron Fude

    Roedy Green Guest

    On Tue, 1 Apr 2008 19:38:34 -0700 (PDT), Aaron Fude
    <> wrote, quoted or indirectly quoted someone who
    said :

    >Is there a more tasteful way of accomplishing this?


    What is distasteful about your existing code. If you want some
    alternatives see http://mindprod.com/jgloss/trace.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Apr 2, 2008
    #6
    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.
Similar Threads
  1. S. Justin Gengo
    Replies:
    0
    Views:
    729
    S. Justin Gengo
    Aug 5, 2003
  2. Karsten Grombach

    Stacktrace with line numbers

    Karsten Grombach, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    461
    Tommy
    Aug 8, 2003
  3. Ken Dopierala Jr.
    Replies:
    0
    Views:
    446
    Ken Dopierala Jr.
    Sep 21, 2004
  4. Sebastien
    Replies:
    3
    Views:
    8,629
    chinnaswamyg
    Sep 28, 2006
  5. chinnaswamyg
    Replies:
    0
    Views:
    667
    chinnaswamyg
    Sep 28, 2006
Loading...

Share This Page