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.
fridi 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

Startups for a stronger Resume?

When the whole web is abuzz with keywords such as Ajax, Web 2.0, Meebo and all, there is some uncertainity about how this new Internet Bubble will shape up.

Mike at Techdirt writes that some of these new startups may be only to actually stand out for the crowd and act as a strong resume.

While the complaint is that so many of these companies are built to flip rather than to be sustainable businesses, some are noticing that this process is really just a better way for some to stand out and get a job. You build a company quickly and cheaply that gets some attention, and a bigger company comes along to scoop up your company, giving you a nice "signing bonus." In effect, the purpose of these companies is to act as a strong resume to stand out from the crowd.
Technorati Tags:

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."

Wimbledon - Swan Lake and Bartoli

OK. In the last few hours, I've been asked this question thrice and hence this post: Who am I rooting for in this Wimbledon? I no longer cheer for "Gentlemen's Tennis" these days. For it is unusually mundane and boring. FedEx and Nadal are way way ahead of anyone else that is clearly reflected in their rankings and hardly makes for any competition. Andy Roddick comes a distant third and this makes watching men's game a little less exciting when compared to the yesteryears of the men's game. The years that passed by had a different air to them, with the likes of Sampras, Agassi, Jim Courier, Goran Ivanisevic taking to the courts, the atmosphere that much more intense, the involvement all that much more. Plus as my friend Santosh says it- where are the serve and volley plays! (For all those jigging about on this stance of mine on FedEX and Tennis, I won't still buy your arguments about Schumi's dominance making F1 mundane though! F1 without Schumi is st…