Difference between e.printStackTrace and System.out.println(e)

前端 未结 6 1517
清歌不尽
清歌不尽 2021-01-31 17:05

Probably a newbie question, but everyone seems to use e.printStackTrace(), but I have always used System.out.println(e) when exception handling. What i

6条回答
  •  旧巷少年郎
    2021-01-31 17:39

    Since the output of e.printStackTrace(); is System.err and usually I output my app log to a file, I recommend you to use both System.err and System.out to output errors.

    public static void log(Exception e) {
        e.printStackTrace(); // This goes to System.err
        e.printStackTrace(System.out);
    }
    

    This way you can see the errors in the log file (in case you have one) and in the console.

提交回复
热议问题