[Java] printStackTrace() over a String

Another little and easy tip I often use. The task is to redirect the Throwable.printStackTrace() over a String object. In this way you can manage the string including it, for example, in an XML stream.

Less words, here it is

// "t" is an instance of Throwable
//...
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
String  error = sw.toString();
pw.close();
sw.close();
//...
Advertisements

One thought on “[Java] printStackTrace() over a String

  1. If you aren’t sure about your job or other source of income for the next couple of weeks. Install a wash-basin with shelves under it where you can store soaps, toothpastes, cosmetics and other such things properly in their assigned places. In some ways, it’s Ferry through and
    through. We also picked up a bottle a of cleaner called Awesome for a dollar which helped to remove the old finishing
    and replace it with a brush and wiping off excess with
    a dry cloth.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s