Skip to main content

Java: Convert StackTrace to String

Okay. This is how you could get a StackTrace and write it to a String:

try
{
//code here
}
catch(Exception e)
{
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String stacktrace = sw.toString();
}

Comments

Anonymous said…
Fine, exactly wehat i was looking for.
By the way, only google hit to the following search terms:

java exception "stacktrace to string"

Thanks
Marcel
enginerd said…
Yep! In fact, this post remains one of the most frequently visited!!!

Glad that you found it useful :)
Anonymous said…
Thank you so much. This was a great help to me.
Is there any way to have the same in J2ME..

Most the VM's on Phone don't even pass the message version of the exception.. and hence at least knowing the stack trace would help a lot..

Regards,
Raja Nagendra Kumar,
C.T.O
www.tejasoft.com
Juanjo Vega said…
Thanks a lot. I was looking for something so simple, not as other complex solutions with classes to convert stack traces.
Anonymous said…
Excellent and pritty simple ;-)
Anonymous said…
Great thanks!! :-)
Tom Jenkinson said…
Thanks!
Anonymous said…
Thanks a lot! Should be part of the API.

Popular posts from this blog

Gmail Chat Disabled

The fact that it happened does not surprise me but that it took so long for our network administrators to figure it out does. And if you are wondering how do you disable Gmail's chat features on your network, you only need locking DNS lookups to chatenabled.mail.google.com , by returning 127.0.0.1 .

Firefox Extension for coComment!

I have put up the Firefox Extension for coComment! I and a few friends have been using it for a day now and it seems to work fine! Download coComment! for Firefox coComment! for Firefox allows you to coComment! from the right-click context menu and is of great help especially when commenting in a pop-up window where you can't access your bookmarks. Also, with this extension you will not have to move from the comment area to your bookmarks to click the 'bookmarklet' and then back, before you click the submit button on the blog. With this extension, you can directly move on to the submit button after writing the comment and merely hit the right click 'activating' coComment before submitting the post. Simple Fitts' law and it makes your browsing experience a great deal better and faster. Let me know what you think of it!