skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Thursday, September 20, 2012

Java Stack Trace at any point for debugging

Posted by Admin at 12:42 PM – 0 comments
 

This code snippet gives us Stack flow at any point in java code execuition irrespective of any errors. This oudl be really helpful where we are new to the project and trying to under stand teh work flow. By invoking this method call , it prints the stack trace , thus giving us clarity on the work flow.



public static void showCallStack() 

{ 
 System.out.println("  Call Stack trace...............n***************************START************n");
 // Will create stack trace of current thread execution
 StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); 
 //for looping  across the stack trace elements   
 for (int i=2 ; i<stackTraceElements.length; i++)        
 { 
  StackTraceElement ste = stackTraceElements[i]; 
  String classname = ste.getClassName();         
  String methodName = ste.getMethodName();          
  int lineNumber = ste.getLineNumber();             
  System.out.println( classname+"."+methodName+":"+lineNumber); 
 } 

 System.out.println("  Call Stack trace ends ...............n**********************************************nnn");  

}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger