Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

StackTrace question

0 views
Skip to first unread message

Rhino

unread,
Nov 9, 2005, 11:48:04 AM11/9/05
to
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


Robert Klemme

unread,
Nov 9, 2005, 11:55:13 AM11/9/05
to

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[i];
System.out.println( element );
}
}
}

Kind regards

robert

Ingo R. Homann

unread,
Nov 9, 2005, 12:05:28 PM11/9/05
to
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[i];
> System.out.println( element );
> }

Indeed, this can be done a little easier:

new Exception().printStackTrace();

Ciao,
Ingo

Roedy Green

unread,
Nov 9, 2005, 12:14:12 PM11/9/05
to
On Wed, 9 Nov 2005 11:48:04 -0500, "Rhino"
<no.offline.c...@nospam.com> 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.

Daniel Dyer

unread,
Nov 9, 2005, 1:04:10 PM11/9/05
to
On Wed, 09 Nov 2005 17:05:28 -0000, Ingo R. Homann <ihoman...@web.de>
wrote:

And even more easily,

Thread.dumpStack();

Dan.

--
Daniel Dyer
http://www.dandyer.co.uk

Steve W. Jackson

unread,
Nov 11, 2005, 2:25:58 PM11/11/05
to
In article <op.szzi4...@cgl0517.chaucer.co.uk>,
"Daniel Dyer" <d...@dannospamformepleasedyer.co.uk> wrote:

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

Oliver Wong

unread,
Nov 11, 2005, 4:13:05 PM11/11/05
to

"Steve W. Jackson" <stevew...@charter.net> wrote in message
news:stevewjackson-6C2...@individual.net...

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


Chris Uppal

unread,
Nov 12, 2005, 6:12:47 AM11/12/05
to
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

0 new messages