Before understanding stack trace let use understands how Java applications are executed. Whenever a java application is executed, Heap is used to allocate memory at run time for various objects; apart from heap run time stack is created which is filled and removed for the methods and constructors calls. When the exception is raised the stack content is printed and hence is known as Stack trace. Looking at stack trace we can find the actual error and how our code reached their. The examples and diagrams are for understanding the concepts only.
Consider simple Java application with main method with simple print statement of HelloWorld. When the program is executed, the execution starts with main method and hence the main method is pushed in run time stack, heap will be used to allocate space for args i.e. Command Line Arguments. Following diagram shows it in better way for understanding.
Apart from what is shown in diagram many other things are maintained by JVM. If the exception occurs we will get exception in thread main followed by exception name and message and in very next line we will get the class name and line number where exception occurred. This is nothing but a stack trace as we can see in above diagram run time stack has only one entry. Line number is printed in stack trace as during execution each stack entry maintains counter showing executing line.
Let us take another example, involving two classes namely Person and Employee, where Employee class inherits the Person. Sample code is shown below
We can see in main method Employee Object is created and call to constructor is made, Employee class constructor calls Person class constructor. Now run time stack looks like following diagram.
Apart from variable that is shown in above diagram, program counter is also maintained by every entry in run time stack.
Seeing above diagram we can conclude that execution started with main as it is the first element in run time stack. From main the employee constructor is called, and from employee constructor person constructor is called and currently it is being executed. At this stage main will have counter pointing to line from where call to Employee constructor is made. Empolyee Constructor will maintain its counter from where the call to Person Constructor is made.
When the Person constructor finishes its execution, it will be removed (pop) from the run time stack and this will give execution control back to Employee Constructor, and Employee constructor will resume its execution using its counter which was saved earlier, than same happens for main. Once the execution of main is completed the stack is empty and program execution is completed.
At any moment top of stack shows currently executing method or constructor.
With same program if we add an erroneous code in Person say int x = 2/0 as shown in following diagram.
When we execute the program it will display some thing like below :
Exception in thread "main" java.lang.ArithmeticException: / by zero
It shows error is in Person code at Line 9 due to Employee code at Line 18 due to main at line 27.
We can say when exception occurred the stack is dumped till end and all the entry is printed with the counter which shows the line of execution.
Hope it clears what is stack trace and what is shows and how to use it for debugging program.