Java StackTraceElement getClassName() Method



Description

The Java StackTraceElement getClassName() method returns the fully qualified name of the class containing the execution point represented by this stack trace element.

Declaration

Following is the declaration for java.lang.StackTraceElement.getClassName() method

public String getClassName()

Parameters

NA

Return Value

This method returns the fully qualified name of the Class containing the execution point represented by this stack trace element.

Exception

NA

Example: Getting Current Thread Class Name using StackTraceElement

The following example shows the usage of Java StackTraceElement getClassName() method. In this program, we've defined a function named function2() which gets the class name from StackTraceElement of current thread. Another function named function1() is used to instantiate the StackTraceElementDemo class and call the function2() method. In main method, we've called the function1() method and result is printed.

package com.tutorialspoint;

public class StackTraceElementDemo {

   // main method
   public static void main(String[] args) {
      // call function1() to invoke function2()     
      function1();
   }
   
   // function1() is to invoke function2()
   // using StackTraceElementDemo object
   public static void function1() {
      new StackTraceElementDemo().function2();
   }
 
   // print the class name of current thread using StackTraceElement
   public void function2() {
      System.out.print("class name : ");
      System.out.print(Thread.currentThread().getStackTrace()[1].
      getClassName());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

class name : com.tutorialspoint.StackTraceElementDemo

Example: Getting Class Name using ArithmeticException's StackTraceElement

The following example shows the usage of Java StackTraceElement getClassName() method. In main method, we've created three int variables where one variable is carrying zero value. In try-catch block, we've created a divide by zero scenario to raise an ArithmeticException. In catch block, we've handled the ArithmeticException, retrieved an array of StackTraceElement from it using getStackTrace() method and then using getClassName() method on first element of the array, the class name is printed.

package com.tutorialspoint;

public class StackTraceElementDemo {

   public static void main(String[] args) {

      // variables to store int values
      int a = 0, b = 1, c;

      try {
         // being divide by zero, this line will throw an ArithmeticException
         c = b / a;
      }catch(ArithmeticException e) {
         // get the array of StackTraceElement
         StackTraceElement[] stackTraceElements =  e.getStackTrace();
         // print the class name
         System.out.println(stackTraceElements[0].getClassName());
      }
   }
}  

Output

Let us compile and run the above program, this will produce the following result −

class name : com.tutorialspoint.StackTraceElementDemo

Example: Getting Class Name using Exception's StackTraceElement

The following example shows the usage of Java StackTraceElement getClassName() method. In main method, we've created three int variables where one variable is carrying zero value. In try-catch block, we've created a divide by zero scenario to raise an ArithmeticException. In catch block, we've handled the Exception, retrieved an array of StackTraceElement from it using getStackTrace() method and then using getClassName() method on first element of the array, the class name is printed.

package com.tutorialspoint;

public class StackTraceElementDemo {

   public static void main(String[] args) {

      // variables to store int values
      int a = 0, b = 1, c;

      try {
         // being divide by zero, this line will throw an ArithmeticException
         c = b / a;
      }catch(Exception e) {
         // get the array of StackTraceElement
         StackTraceElement[] stackTraceElements =  e.getStackTrace();
         // print the class name
         System.out.println(stackTraceElements[0].getClassName());
      }
   }
}  

Output

Let us compile and run the above program, this will produce the following result −

class name : com.tutorialspoint.StackTraceElementDemo
java_lang_stacktraceelement.htm
Advertisements