Print the stack trace of an exception

JavaException

Java Problem Overview


How do I print the stack trace of an exception to a stream other than stderr? One way I found is to use getStackTrace() and print the entire list to the stream.

Java Solutions


Solution 1 - Java

Not beautiful, but a solution nonetheless:

StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
exception.printStackTrace( printWriter );
printWriter.flush();

String stackTrace = writer.toString();

Solution 2 - Java

There is an alternate form of Throwable.printStackTrace() that takes a print stream as an argument. <http://download.oracle.com/javase/6/docs/api/java/lang/Throwable.html#printStackTrace(java.io.PrintStream)>

E.g.

catch(Exception e) {
    e.printStackTrace(System.out);
}

This will print the stack trace to std out instead of std error.

Solution 3 - Java

Throwable.printStackTrace(..) can take a PrintWriter or PrintStream argument:

} catch (Exception ex) {
    ex.printStackTrace(new java.io.PrintStream(yourOutputStream));
}

That said, consider using a logger interface like SLF4J with an logging implementation like LOGBack or log4j.

Solution 4 - Java

For the android dev minimalists: Log.getStackTraceString(exception)

Solution 5 - Java

Apache commons provides utility to convert the stack trace from throwable to string.

Usage:

ExceptionUtils.getStackTrace(e)

For complete documentation refer to https://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html

Solution 6 - Java

I have created a method that helps with getting the stackTrace:

private static String getStackTrace(Exception ex) {
    StringBuffer sb = new StringBuffer(500);
    StackTraceElement[] st = ex.getStackTrace();
    sb.append(ex.getClass().getName() + ": " + ex.getMessage() + "\n");
    for (int i = 0; i < st.length; i++) {
      sb.append("\t at " + st[i].toString() + "\n");
    }
    return sb.toString();
}

Solution 7 - Java

The Throwable class provides two methods named printStackTrace, one that accepts a PrintWriter and one that takes in a PrintStream, that outputs the stack trace to the given stream. Consider using one of these.

Solution 8 - Java

See javadoc

out = some stream ...
try
{
}
catch ( Exception cause )
{
      cause . printStrackTrace ( new PrintStream ( out ) ) ;
}

Solution 9 - Java

If you are interested in a more compact stack trace with more information (package detail) that looks like:

  java.net.SocketTimeoutException:Receive timed out
  	at j.n.PlainDatagramSocketImpl.receive0(Native Method)[na:1.8.0_151]
  	at j.n.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:143)[^]
  	at j.n.DatagramSocket.receive(DatagramSocket.java:812)[^]
  	at o.s.n.SntpClient.requestTime(SntpClient.java:213)[classes/]
  	at o.s.n.SntpClient$1.call(^:145)[^]
  	at ^.call(^:134)[^]
  	at o.s.f.SyncRetryExecutor.call(SyncRetryExecutor.java:124)[^]
  	at o.s.f.RetryPolicy.call(RetryPolicy.java:105)[^]
  	at o.s.f.SyncRetryExecutor.call(SyncRetryExecutor.java:59)[^]
  	at o.s.n.SntpClient.requestTimeHA(SntpClient.java:134)[^]
  	at ^.requestTimeHA(^:122)[^]
  	at o.s.n.SntpClientTest.test2h(SntpClientTest.java:89)[test-classes/]
  	at s.r.NativeMethodAccessorImpl.invoke0(Native Method)[na:1.8.0_151]

you can try to use Throwables.writeTo from the spf4j lib.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionr.vView Question on Stackoverflow
Solution 1 - JavaMaurício LinharesView Answer on Stackoverflow
Solution 2 - JavaMike DeckView Answer on Stackoverflow
Solution 3 - JavaBozhoView Answer on Stackoverflow
Solution 4 - JavaPatrickMAView Answer on Stackoverflow
Solution 5 - JavaRathanView Answer on Stackoverflow
Solution 6 - JavakuppurangiView Answer on Stackoverflow
Solution 7 - JavatemplatetypedefView Answer on Stackoverflow
Solution 8 - JavaemoryView Answer on Stackoverflow
Solution 9 - Javauser2179737View Answer on Stackoverflow