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 .

Advertising Billboards as Rain Covers

Advertising billboards are put to use as Pakistani refugees, left homeless after the October 8 earthquake, set up their tents in Muzaffarabad, Pakistan. [via SFGate ] Technorati Tags: Pakistan Earthquake

Community effort to create a single persistence model for the Java community

A community effort led by Sun Microsystems is aiming to create a single 'Plain Old Java Object' persistence model to provide a single object/relational mapping facility for Java app developers in J2SE and J2EE. Paul Krill writes In a letter to the “Java Technology Community” on Friday, specification leads on Java Specification Request (JSR) 220, which is the proposal for Enterprise JavaBeans 3.0, and JSR-243, for Java Data Objects, state that the two technologies feature divergent persistence models. “This divergence has caused confusion and debates among Java developers, and is not in the best of interest of the Java community,” said JSR-220 leader Linda DeMichiel who also is a Sun employee, and Craig Russell, a staff engineer at Sun who leads JSR-243. “In response to these requests [for an end to the unwanted divide], Sun Microsystems is leading a community effort to create a single POJO (Plain Old Java Object) persistence model for the Java community,” the letter said.