Friday, May 7, 2010

Get stack trace and follow your execution path

Sometimes you don't have enough time to analyse source application architecture/UML diagrams etc.
If it is an urgent issue (needed to be FIX asap like always :-p) you want to learn program execution path to understand what it is doing !

new Throwable ().printStackTrace(); gives you what you want

Lets have a look at it.

public class GetStackTrace {
public static void main (String[] args) {
GetStackTrace obj = new GetStackTrace ();
obj.methodOne();
}
public void methodOne (){
System.out.println (" -- im in the method one.");
methodSeven ();
}
public void methodTwo (){
System.out.println (" -- im in the method two.");
methodFour();
}
public void methodThree (){
System.out.println (" -- im in the method three.");
}
public void methodFour (){
System.out.println (" -- im in the method four.");
methodSix ();
}
public void methodFive (){
System.out.println (" -- im in the method five.");
}
public void methodSix (){
System.out.println (" -- im in the method six.");
new Throwable ().printStackTrace();
methodFive ();
}
public void methodSeven (){
System.out.println (" -- im in the method seven.");
methodTwo ();
}
}


And it's output is here :


-- im in the method one.
-- im in the method seven.
-- im in the method two.
-- im in the method four.
-- im in the method six.
java.lang.Throwable
at GetStackTrace.methodSix(GetStackTrace.java:26)
at GetStackTrace.methodFour(GetStackTrace.java:19)
at GetStackTrace.methodTwo(GetStackTrace.java:12)
at GetStackTrace.methodSeven(GetStackTrace.java:31)
at GetStackTrace.methodOne(GetStackTrace.java:8)
at GetStackTrace.main(GetStackTrace.java:4)
-- im in the method five.

No comments:

Post a Comment