How can I get the current stack trace in Java?

前端 未结 21 3247
耶瑟儿~
耶瑟儿~ 2020-11-21 23:49

How do I get the current stack trace in Java, like how in .NET you can do Environment.StackTrace?

I found Thread.dumpStack() but it is not what I want -

21条回答
  •  醉话见心
    2020-11-22 00:18

    Maybe you could try this:

    catch(Exception e)
    {
        StringWriter writer = new StringWriter();
        PrintWriter pw = new PrintWriter(writer);
        e.printStackTrace(pw);
        String errorDetail = writer.toString();
    }
    

    The string 'errorDetail' contains the stacktrace.

提交回复
热议问题