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.
-
If you want to get exception trace as String you can call `getStackTrace` method of Trowable (the Exception) that will return array of `StackTraceElement` objects that you can combine to one String (using toString method of that object to get one line of a trace). – jcubic Dec 04 '13 at 07:34
10 Answers
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.

- 18,045
- 16
- 68
- 92
-
4@FranklinYu, the whole point of the question is that he doesn't want to print to stderr, but to another arbitrary stream. – Mike Deck Feb 16 '16 at 16:34
-
instead of `System.out` you can use `e.printStackTrace(new PrintStream("path.txt"))` – Mauricio Gracia Gutierrez Jun 30 '22 at 05:48
Not beautiful, but a solution nonetheless:
StringWriter writer = new StringWriter();
PrintWriter printWriter = new PrintWriter( writer );
exception.printStackTrace( printWriter );
printWriter.flush();
String stackTrace = writer.toString();

- 39,901
- 14
- 121
- 158
-
2how would we print this with log4j? log.error(ex.printStackTrace())? – Geoff Langenderfer Jun 17 '22 at 00:06
For the android dev minimalists: Log.getStackTraceString(exception)

- 887
- 9
- 23
-
1You can't use this to print to an arbitrary stream. It is useful only if you have a logger configured. – r.v Dec 06 '15 at 21:38
-
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

- 419
- 6
- 6
-
While this answer looks very much correct, it is not an answer to the question. OP asked how he could print the stack trace to another stream. – Buurman Mar 20 '18 at 16:10
-
Even though this might not answer OP's question, I think this is the best way to have an exception as a String, using a very well established format. Thanks! – Clint Eastwood Feb 20 '19 at 13:57
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();
}

- 1,304
- 1
- 11
- 29

- 111
- 1
- 2
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.

- 362,284
- 104
- 897
- 1,065
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.

- 493
- 3
- 6