StackTrace question

Discussion in 'Java' started by Rhino, Nov 9, 2005.

  1. Rhino

    Rhino Guest

    Is there any way to print a stack trace if no exception has taken place?

    It seems like there should be some simple way to write a stack trace to the
    console any time you like, even if an exception has not occurred but I'm
    blanking out when I try to think of how that could be done. I feel sure it
    is possible and is easy so maybe my brain is just not firing on all
    cylinders today....

    Can anyone help?

    --
    Rhino
    Rhino, Nov 9, 2005
    #1
    1. Advertising

  2. Rhino wrote:
    > Is there any way to print a stack trace if no exception has taken
    > place?
    >
    > It seems like there should be some simple way to write a stack trace
    > to the console any time you like, even if an exception has not
    > occurred but I'm blanking out when I try to think of how that could
    > be done. I feel sure it is possible and is easy so maybe my brain is
    > just not firing on all cylinders today....
    >
    > Can anyone help?


    There's a fairly easy workaround:

    package exceptions;

    public class PrintStackTrace {

    public static void main( String[] args ) {
    test();
    }

    private static void test() {
    Throwable t = new Throwable();
    t.fillInStackTrace();
    StackTraceElement[] stackTrace = t.getStackTrace();
    for ( int i = 0; i < stackTrace.length; ++i ) {
    StackTraceElement element = stackTrace;
    System.out.println( element );
    }
    }
    }

    Kind regards

    robert
    Robert Klemme, Nov 9, 2005
    #2
    1. Advertising

  3. Hi,

    Robert Klemme wrote:
    > Throwable t = new Throwable();
    > t.fillInStackTrace();
    > StackTraceElement[] stackTrace = t.getStackTrace();
    > for ( int i = 0; i < stackTrace.length; ++i ) {
    > StackTraceElement element = stackTrace;
    > System.out.println( element );
    > }


    Indeed, this can be done a little easier:

    new Exception().printStackTrace();

    Ciao,
    Ingo
    Ingo R. Homann, Nov 9, 2005
    #3
  4. Rhino

    Roedy Green Guest

    On Wed, 9 Nov 2005 11:48:04 -0500, "Rhino"
    <> wrote, quoted or indirectly
    quoted someone who said :

    >Is there any way to print a stack trace if no exception has taken place?

    see http://mindprod.com/jgloss/trace.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 9, 2005
    #4
  5. Rhino

    Daniel Dyer Guest

    On Wed, 09 Nov 2005 17:05:28 -0000, Ingo R. Homann <>
    wrote:

    > Hi,
    >
    > Robert Klemme wrote:
    >> Throwable t = new Throwable();
    >> t.fillInStackTrace();
    >> StackTraceElement[] stackTrace = t.getStackTrace();
    >> for ( int i = 0; i < stackTrace.length; ++i ) {
    >> StackTraceElement element = stackTrace;
    >> System.out.println( element );
    >> }

    >
    > Indeed, this can be done a little easier:
    >
    > new Exception().printStackTrace();
    >
    > Ciao,
    > Ingo


    And even more easily,

    Thread.dumpStack();

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Nov 9, 2005
    #5
  6. In article <>,
    "Daniel Dyer" <> wrote:

    > On Wed, 09 Nov 2005 17:05:28 -0000, Ingo R. Homann <>
    > wrote:
    >
    > > Hi,
    > >
    > > Robert Klemme wrote:
    > >> Throwable t = new Throwable();
    > >> t.fillInStackTrace();
    > >> StackTraceElement[] stackTrace = t.getStackTrace();
    > >> for ( int i = 0; i < stackTrace.length; ++i ) {
    > >> StackTraceElement element = stackTrace;
    > >> System.out.println( element );
    > >> }

    > >
    > > Indeed, this can be done a little easier:
    > >
    > > new Exception().printStackTrace();
    > >
    > > Ciao,
    > > Ingo

    >
    > And even more easily,
    >
    > Thread.dumpStack();
    >
    > Dan.


    Amusingly enough, Sun's source code shows that the Thread.dumpStack
    method does the same thing, except that it gives the Exception's
    constructor a String parameter saying "Stack Trace".

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Nov 11, 2005
    #6
  7. Rhino

    Oliver Wong Guest

    "Steve W. Jackson" <> wrote in message
    news:...
    >
    > Amusingly enough, Sun's source code shows that the Thread.dumpStack
    > method does the same thing, except that it gives the Exception's
    > constructor a String parameter saying "Stack Trace".


    I imagine this happened after lots of developpers asked for a way to get
    the stack trace and balked when Sun told them to just create a new Exception
    ("isn't that wasteful?!" they'd complain). So to placate them, Sun created
    this new method and everyone lived happily ever after.

    - Oliver
    Oliver Wong, Nov 11, 2005
    #7
  8. Rhino

    Chris Uppal Guest

    Oliver Wong did not, in fact, write:
    > > Amusingly enough, Sun's source code shows that the Thread.dumpStack
    > > method does the same thing, except that it gives the Exception's
    > > constructor a String parameter saying "Stack Trace".


    That /is/ amusing.

    But Oliver did write:
    > I imagine this happened after lots of developpers asked for a way to
    > get the stack trace and balked when Sun told them to just create a new
    > Exception ("isn't that wasteful?!" they'd complain). So to placate them,
    > Sun created this new method and everyone lived happily ever after.


    Has the advantage too that the functionality is now available in the "right"
    place. And Sun can, at a later date, move the actual implementation too.

    -- chris
    Chris Uppal, Nov 12, 2005
    #8
    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:
    701
    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:
    434
    Tommy
    Aug 8, 2003
  3. Ken Dopierala Jr.
    Replies:
    0
    Views:
    423
    Ken Dopierala Jr.
    Sep 21, 2004
  4. =?Utf-8?B?U3RldmUgS2FsbGFs?=

    StackTrace Line Numbers Missing in ASP.NET deployment...

    =?Utf-8?B?U3RldmUgS2FsbGFs?=, Sep 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,428
    =?Utf-8?B?U3RldmUgS2FsbGFs?=
    Sep 20, 2004
  5. Kieran Toon
    Replies:
    0
    Views:
    436
    Kieran Toon
    Mar 1, 2006
Loading...

Share This Page