StackTrace question

R

Rhino

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?
 
R

Robert Klemme

Rhino said:
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
 
I

Ingo R. Homann

Hi,

Robert said:
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
 
D

Daniel Dyer

Hi,

Robert said:
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.
 
S

Steve W. Jackson

Daniel Dyer said:
Hi,

Robert said:
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 =
 
O

Oliver Wong

Steve W. Jackson said:
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
 
C

Chris Uppal

Oliver Wong did not, in fact, write:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top