How do I write the exception from printStackTrace() into a text file in Java?

后端 未结 5 1098
故里飘歌
故里飘歌 2020-12-03 03:00

I need to capture the exception in a text file in Java. For example:

try {
  File f = new File(\"\");
}
catch(FileNotFoundException f) {
  f.printStackTrac         


        
5条回答
  •  -上瘾入骨i
    2020-12-03 03:39

    There is an API in Throwable interface getStackTrace() which is used internally for printing in console by printStackTrace()

    http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Throwable.html#getStackTrace()

    Try this API to get StackTraceElement and print those elements sequentially.

提交回复
热议问题