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 .

Pottermania

I'll have to admit that the nature of Pottermania is beyond my comprehension capabilities! As more and more fans of all ages and countries immerse themselves in Harry’s adventures, adding fuel to the belief that Pottermania is inescapable, I am purely and simply baffled by its ability to appeal to all ages. Bored by the inactivity prevailing in this muggle world, I began to read the first one of the series and immediately concluded that reading beyond the tenth leaf was something impossible. Undeterred, I watched the first movie. I Hated it. But with the evil-wizard becoming apple of more and more eyes with every passing moment, around the world, it could only have been something wrong with me and so I watched the second movie. And then it became so easy to establish that I loathed the Potter Kid! Just a passing thought - "Would it be a bad idea for a Potter special appearance in Oliver Twist? The poor kid could do with some wizardry."

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!