Friday, 28 February 2014

What is a Stack trace element

Each line in the stack trace represents Stack Trace Element. 

class StackTraceElement{
 void divide(){
  int var1 = 10/0;
 }

 void show(){
  divide();
 }

 void print(){
  show();
 }

 public static void main(String args[]){
  StackTraceElement obj = new StackTraceElement();
  obj.print();
 }
}
   
When you run the above program, it generates “ArithmeticException” and JVM throws the stack trace like below.

Exception in thread "main" java.lang.ArithmeticException: / by zero
          at StackTraceElement.divide(StackTraceElement.java:4)
          at StackTraceElement.show(StackTraceElement.java:8)
          at StackTraceElement.print(StackTraceElement.java:12)
          at StackTraceElement.main(StackTraceElement.java:17)


Each line in the stack trace represents Stack Trace Element.
For Ex:
"StackTraceElement.divide(StackTraceElement.java:4)" is a Stack Trace Element.


Overriding vs Exception Handling                                                 Create Stack trace element                                                 Home

No comments:

Post a Comment