Is there a way to dump a stack trace without throwing an exception in java?

前端 未结 10 1433
庸人自扰
庸人自扰 2020-12-04 07:37

I am thinking of creating a debug tool for my Java application.

I am wondering if it is possible to get a stack trace, just like Exception.printStackTrace()

10条回答
  •  北荒
    北荒 (楼主)
    2020-12-04 08:16

    If you need capture output

    StringWriter sw = new StringWriter();
    new Throwable("").printStackTrace(new PrintWriter(sw));
    String stackTrace = sw.toString();
    

提交回复
热议问题