How to print a stack trace without throwing an exception?

Discussion in 'Java' started by laredotornado, Jul 29, 2008.

  1. Hi,

    I'm at a place in my code and trying to figure out how the program
    reached there. I can log messages all day, but ultimately it would be
    great if there were something that dump a stack trace at whereever I
    was without halting program execution. Any suggestions? I'm using
    Java 1.5 on a WebLogic 9.2.2 server.

    Thanks, - Dave
    laredotornado, Jul 29, 2008
    1. Advertisements

  2. laredotornado

    Donkey Hot Guest

    public class TraceUtil
    static void printStackTrace()
    throw new Exception() ;
    catch (Exception ex)
    ex.printTrackTrace() ;


    TraceUtil.printStackTrace() ;
    Donkey Hot, Jul 29, 2008
    1. Advertisements

  3. The easiest thing to do is this:

    public void printStackTrace() {
    try {
    throw new Exception();
    } catch (Exception e) {

    You can also do:
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();

    and manually parse the stack trace element. The latter form also has a
    way to print all stack traces of all threads. Note, however, that the
    stack traces may omit some frames.
    Joshua Cranmer, Jul 29, 2008
  4. Thread.dumpStack()
    should be preferred over
    new Exception().printStackTrace()
    IMHO, as it might be optimised in future releases.
    Daniele Futtorovic, Jul 29, 2008
  5. laredotornado

    Roedy Green Guest

    Roedy Green, Jul 29, 2008
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.