简体   繁体   中英

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"). 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)

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).

* 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. I'm probably just tired.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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