Click to See Complete Forum and Search --> : redirect Throwable.printStackTrace()...


Frederik
September 15th, 2000, 06:46 AM
basicly i want to redirect the the Throwable.printStackTrace() / printStackTrace(PrintStream) / printStackTrace(PrintWriter) to a String / StringBuffer instead of System.err.

Does anyone know how to do this? plz help :)

Norm
September 15th, 2000, 11:37 AM
There is the System.setErr(PrintStream) method that will change the StdErr to go to a PrintStream. Then that PrintStream could be a connected to a StringWriter.

Frederik
September 16th, 2000, 08:25 AM
Finally got it to work. Here's the code if someone else needs it...

try
{
...
}
catch(Throwable e)
{
StringWriter errorBuffer = new StringWriter();
e.printStackTrace(new PrintWriter(errorBuffer));
}

errorBuffer now contains the stacktrace in memory, and you can do whatever you want with it ;)