Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

storing stack trace into string

 
Kripal Singh
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.
thanks,
kripal
 
Chris Stehno
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
yourException.printStackTrace(printWriter);
anotherException.printStackTrace(printWriter);
... 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)
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or to simplify...

Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic