Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

StackTrace question

Thread view: 
Rhino - 09 Nov 2005 16:48 GMT
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?

Signature

Rhino

Robert Klemme - 09 Nov 2005 16:55 GMT
> Is there any way to print a stack trace if no exception has taken
> place?
[quoted text clipped - 6 lines]
>
> 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[i];
           System.out.println( element );
       }
   }
}

Kind regards

   robert
Ingo R. Homann - 09 Nov 2005 17:05 GMT
Hi,

>         Throwable t = new Throwable();
>         t.fillInStackTrace();
[quoted text clipped - 3 lines]
>             System.out.println( element );
>         }

Indeed, this can be done a little easier:

new Exception().printStackTrace();

Ciao,
Ingo
Daniel Dyer - 09 Nov 2005 18:04 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> Ciao,
> Ingo

And even more easily,

    Thread.dumpStack();

Dan.

Signature

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

Steve W. Jackson - 11 Nov 2005 19:25 GMT
> > Hi,
> >
[quoted text clipped - 18 lines]
>
> 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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Oliver Wong - 11 Nov 2005 21:13 GMT
> 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 - 12 Nov 2005 11:10 GMT
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
Roedy Green - 09 Nov 2005 17:14 GMT
On Wed, 9 Nov 2005 11:48:04 -0500, "Rhino"
<no.offline.contact.please@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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.