java printStackTrace()

February 18, 2009 at 14:52:26
Specs: Windows 2000, 2G
In java, we can call printStackTrace() method from exception object, such as:

Exception ex = new Exception();
ex.printStackTrace();

I remembered I used to call printStackTrace() from system.out.... I cannot remmber now. How can I print call hirerachy if I don't use exception object?

Thanks!


See More: java printStackTrace()

Report •


#1
February 19, 2009 at 07:38:40
I can't think of any way of doing it without using an Exception object (or at least a Throwable, from which Exception is derived) but here's one way to print a stack trace:

    for (StackTraceElement e : new Throwable().getStackTrace())
        System.out.println(e);


Report •

#2
February 19, 2009 at 09:06:35
Thanks for your reply. I have recalled another way of doing this:

Thread.dumpStack();


Report •

#3
February 19, 2009 at 09:18:01
Thanks for sharing, that's much better.

Report •

Related Solutions


Ask Question