redirecting throwable.printStackTrace

I would like to re-direct the stack trace of an exception to a String (so that I can write it to a log file).
Ideally, I would like a PrintStream class that writes to a String buffer.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you look at  java.lang.trowable.printStackTrace docs, you will find an impementation for:

public void printStackTrace(PrintStream s)
Prints this Throwable and its backtrace to the  specified print stream.  

By using this method, you can create a PrintStream derived class that writes to any place you want (disk, printer, stdout, etc) and solve your problem. The trick is to use the throwable implementation.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sprinkmeierAuthor Commented:
OK, I know hot to print the stack trace to a PrintStream, but how do I create a Printstream that outputs to a String buffer?

I know it's in the DOC's somewhere, and if I had them, I would not be asking this question.

ByteArrayOutputStream out = new ByteArrayOutputStream();

PrintStream ps = new PrintStream(out);

// You now have a Print Stream that writes to a buffer array.


// Inside an exception handler you could now use:


String Buffer = ps.toString();

// Now you can use Buffer in whatever way you require.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

sprinkmeierAuthor Commented:

To Compile:
To Run:
  java  exc_2_string

class exc_2_string

  public static void main(String args[])
      System.out.println("Throwing a wobbly");
      // create and throw an exception
      Exception x = new Exception("ASDF");
    } catch (Exception x) {
      System.out.println("Exception caught");
      // Create a place to stick the data out = new;
/*          ps = new;

  PrintStream is replaced, use PrintWriter instead. Only differance
  seems to be that the PrintWriter must be flush()-ed before you
  examine the ByteArrayOutputStream (alternativley, create the
  PrintWriter with the boolean autoFlush set to reue, ie. pw = new, true);
      // create a stream to accept the data pw = new;
      // write to the stream
      // make sure the stream is flushed
      // copy the data from the storage object
      String buff = out.toString();
      // cleanup
      pw = null;
      out = null;
      // print it
      System.out.println("Via String: " + buff);

      System.out.println("Direct to System.out:");

Hum? What do you want me to do with this code ? :-)
sprinkmeierAuthor Commented:
Just thought I'd give value for money to anyone who buys this question later.

OH, Good Idea.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.