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

前端 未结 10 1406
庸人自扰
庸人自扰 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:07

    The answer by Rob Di Marco is by far the best if you are happy to output to stderr.

    If you want to capture to a String, with new lines as per a normal trace, you could do this:

    Arrays.toString(Thread.currentThread().getStackTrace()).replace( ',', '\n' );
    

提交回复
热议问题