简体   繁体   English

如何打印堆栈跟踪而不会发生错误

[英]How to print stack trace without error occurance

Say I have method "a" inside which I have a if condition and in the else block of this if ,I just print log.warn("predefined message"). 假设我有方法“a”,其中我有if条件,并且在if的else块中,我只打印log.warn(“预定义消息”)。 I want to print stack trace to get where exactly this method is being called and condition got failed. 我想打印堆栈跟踪以获取正确调用此方法的条件并且条件失败。

sudo 须藤

enter code here

class test{
 testa(){
   if(condition is true){
       //do the stuff
     }else {
  log.warn("condition failed "); 
  }
   }
      }

I want to print stack trace with log.warn.(I don't want to use Thread.currentThread().getStackTrace(),any other way to do this whitout using Threads) 我想用log.warn打印堆栈跟踪。(我不想使用Thread.currentThread()。getStackTrace(),任何其他使用Threads执行此操作的方法)

You can create a Throwable (note that I'm not saying throw one, just create it) and call fillInStackTrace *, then use getStackTrace (or whatever you want to do at that point). 你可以创建一个Throwable (注意我不是说抛出一个,只是创建它)并调用fillInStackTrace *,然后使用getStackTrace (或者你想要做的任何事情)。

* I think you need to call fillInStackTrace , but the constructor docs seem to suggest the constructor will call it, which makes me wonder why it would be a public method. *我认为你需要调用fillInStackTrace ,但构造函数docs似乎暗示构造函数会调用它,这让我想知道它为什么会是一个公共方法。 I'm probably just tired. 我可能只是累了。

您可以使用Thread.currentThread().getStackTrace()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM