Print the stack trace of an exception

后端 未结 9 1214
有刺的猬
有刺的猬 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:17

    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.

提交回复
热议问题