Print the stack trace of an exception

后端 未结 9 1150
有刺的猬
有刺的猬 2020-12-05 03:31

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.

9条回答
  •  有刺的猬
    2020-12-05 04:12

    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.

提交回复
热议问题