how to print an exception using logger?

前端 未结 4 2138
无人共我
无人共我 2021-02-18 15:15

I have a situation in which I want to print all the exception caught in catch block using logger.

 try {
        File file = new File(\"C:\\\\className\").mkdir(         


        
4条回答
  •  礼貌的吻别
    2021-02-18 15:27

    You can use this method to log the exception stack to String

     public String stackTraceToString(Throwable e) {
        StringBuilder sb = new StringBuilder();
        for (StackTraceElement element : e.getStackTrace()) {
            sb.append(element.toString());
            sb.append("\n");
        }
        return sb.toString();
    }
    

提交回复
热议问题