Java.lang.StackTraceElement.toString() Method


Advertisements


Description

The java.lang.StackTraceElement.toString() method returns a string representation of this stack trace element.

Declaration

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

public String toString()

Parameters

NA

Return Value

This method returns a string representation of the object.

Exception

NA

Example

The following example shows the usage of java.lang.StackTraceElement.toString() method.

package com.tutorialspoint;

import java.lang.*;

public class StackTraceElementDemo {

   public static void main(String[] args) {
      function1();
   }

   public static void function1() {
      new StackTraceElementDemo().function2();
   }
 
   public void function2() {
      int i;
    
      // print stack trace
      for(i = 1 ; i < 3 ; i++ ) {
         System.out.println(Thread.currentThread().getStackTrace()[i].
         toString());
      }
   }
}   

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

com.tutorialspoint.StackTraceElementDemo.function2(StackTraceElementDemo.java:20)
com.tutorialspoint.StackTraceElementDemo.function1(StackTraceElementDemo.java:12)

java_lang_stacktraceelement.htm

Advertisements