The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2001



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.



Posted by Matt Gerrans on December 03, 2001 at 4:00 PM

Here's how I did it. Maybe you can try it out and see if it works for you.

static String getStackTrace( Throwable t )
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(bytes);
t.printStackTrace( pw );
return bytes.toString();

- mfg

> Hi!
> I wanna catch an SQLException and print its stack trace in a JTextArea rather than the standart output. The method Throwable.getMessage() do not give enough details. I need a procedure like this:
> private String getExceptionStack(Throwable t)
> I try to redirect the stream using the method Throwable.printStackTrace(PrintStream stream)
> Here is my method:

> private String getExceptionStack(Throwable exc)
> {
> try
> {
> String result=null;
> PipedInputStream pInput=new PipedInputStream();
> PipedOutputStream pOutput=new PipedOutputStream(pInput);
> PrintStream pw=new PrintStream(pOutput);
> exc.printStackTrace(pw);
> byte b[]=new byte[pInput.available()];
> result=new String(b);
> return result;
> }
> catch (IOException e)
> {
> return "IOException occured while trying to extract stack!";
> }
> }

> Sometimes it works but sometimes

> exc.printStackTrace(pw);

> blocks and won't return!!! And my thread is deadlocked :(

> I wanna know why and I'm interested in another implementation of this problem.

> Tx!


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us