e.printStackTrace(); in string

前端 未结 5 1332
执笔经年
执笔经年 2021-01-30 13:00

There are e.printStackTrace() method to print exceptional error, so I would like to take entire exception in String and show it by Toast.makeText

5条回答
  •  春和景丽
    2021-01-30 13:40

    you can print the stack trace to a stream & read from it.

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(baos);
    
    e.printStackTrace(pw);
    String stachTrace = new String(baos.toByteArray());
    

    or you can use a StringWriter in place of the ByteArrayOutputStream.

提交回复
热议问题