How to print a stack trace without throwing an exception?

L

laredotornado

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
 
D

Donkey Hot

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

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

...

TraceUtil.printStackTrace() ;
...
 
J

Joshua Cranmer

laredotornado said:
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.

The easiest thing to do is this:

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

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.
 
D

Daniele Futtorovic

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

Thread.dumpStack()
should be preferred over
new Exception().printStackTrace()
IMHO, as it might be optimised in future releases.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top