File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes storing stack trace into string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "storing stack trace into string" Watch "storing stack trace into string" New topic

storing stack trace into string

Kripal Singh
Ranch Hand

Joined: Jul 26, 2001
Posts: 254
I am trying to store the content stack trace for Throwable object into a string object.
method printStackTrace() only prints it and method toString() only returns the first line .
how can i store the full content into a string.

# Help an unprivileged kid.<br /> Whatever u do will make a difference...<br /> a child's life & ur own #<br /><a href="" target="_blank" rel="nofollow"></a>
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
I go that to work once. If I remember right ... Try something like this ...
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
... some misc code ...
... some more misc code ...
String traces = writer.toString();
Since the printStackTrace() method takes a PrintWriter as an argument, you can create a StringWriter and fill it with your stack trace(s) and then convert them to a String later.
Hope this helps.

Chris Stehno (Sun Certified Programmer for the Java 2 Platform)

- Chris Stehno, SCPJ
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Or to simplify...

I agree. Here's the link:
subject: storing stack trace into string
It's not a secret anymore!