Showing posts with label stack trace element. Show all posts
Showing posts with label stack trace element. Show all posts

Friday, 28 February 2014

Extract information from StackTraceElement

StackTraceElement class provides method to extract the class name, method name, line number , file name from the given stack trace element.

public String getClassName()
Returns the fully qualified name of the class

public String getMethodName()
Returns the name of the method containing the execution point represented by this stack trace element.

public String getFileName()
Returns the name of the source file containing the execution point represented by this stack trace element.

public int getLineNumber()
Returns the line number of the source line containing the execution point represented by this stack trace element.

Example
class StackTraceEx{
 void divide(){
  try{
   int val = 10/0;
  }
  catch(ArithmeticException e){
   throw e;
  }
 }

 void show(){
  divide();
 }

 void print(){
  show();
 }

 public static void main(String args[]){
  StackTraceEx obj = new StackTraceEx();

  try{
   obj.print();
  }
  catch(Exception e){
   StackTraceElement[] trace = e.getStackTrace();
   for(int i=0; i < trace.length-1; i++)
    System.out.println(trace[i].getClassName() +" " + trace[i].getFileName() +" " + trace[i].getMethodName() +" " + trace[i].getLineNumber());
   }
  }
}


Output
StackTraceEx StackTraceEx.java divide 15
StackTraceEx StackTraceEx.java show 22
StackTraceEx StackTraceEx.java print 25
   


Create stack trace element                                                 Threads in Java                                                 Home

How to create a stack trace element

Java provides StackTraceElement class to create stack trace element and to extract the details from stack trace element.

By using the constructor provided by the class StackTraceElement, we can create StackTraceElement.

public StackTraceElement(String declaringClass,
String methodName,
String fileName,
int lineNumber)

declaringClass - the fully qualified name of the class containing the execution point represented by the stack trace element

methodName - the name of the method containing the execution point represented by the stack trace element

fileName - the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable

lineNumber - the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method

Example
class StackTraceEx{
 static void show(){
  String className = "StackTraceEx";
  String methodName = "show";
  String fileName = "StackTraceEx.java";
  int lineNum = 9;
  StackTraceElement s1 = new StackTraceElement(className, methodName, fileName, lineNum);
  System.out.println(s1);
 }

 static void print(){
  show();
 }

 public static void main(String args[]){
  StackTraceEx obj1 = new StackTraceEx();
  print();
 }
}
   
Output
StackTraceEx.show(StackTraceEx.java:9)
  

Stack trace Element                                                 Extract information from stack trace element                                                 Home

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