Home » Java programming language

Java Thread Class StackTraceElement[] getStackTrace() method with Example

Java Thread Class StackTraceElement[] getStackTrace() method: Here, we are going to learn about the StackTraceElement[] getStackTrace() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 20, 2019

Thread Class StackTraceElement[] getStackTrace()

  • This method is available in package java.lang.Thread.getStackTrace().
  • This method is used to return an array of stack trace elements that denote the stack dump of the thread.
  • This method is not static so this method is accessible with Thread class object it is not accessible with the class name.
  • This method returns an array of stack trace element and stack has two terms top of the stack and bottom of the stack. Top of the stack contains the method which is called in the last of the hierarchy or sequence and Bottom of the stack contains the method which is called in the first of the hierarchy or sequence.
  • The return type of this method is StackTraceElement[], so it returns an array of stack trace elements.
  • This method raises an exception if the checkPermission method is not allowed to get the stack trace element.

Syntax:

    StackTraceElement[] getStackTrace(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the Thread.

Return value:

The return type of this method is StackTraceElement[], it returns an array of stack trace elements and each stack element denotes one stack frame.

Java program to demonstrate example of getStackTrace() method

/*  We will use Thread class methods so we are importing 
    the package but it is not mandate because 
    it is imported by default
*/

import java.lang.Thread;

class GetStackTrace {
    public static void main(String[] args) {
        // We are calling main1() method from main() method 
        // and this method will call first
        main1();
    }

    public static void main1() {
        // We are calling main2() method from main1() method and 
        // this method will call after first method main1() called 
        // [i.e This method main2() at the second position
        main2();
    }

    public static void main2() {
        // We are calling main3() method from main2() method and 
        // this method will call after second method main2() called
        // [i.e This method main3() will call at the third position
        main3();
    }

    public static void main3() {
        // Creating a reference for Stack Trace Element
        StackTraceElement[] ste = Thread.currentThread().getStackTrace();
        System.out.println("We are using for loop to display stack trace elements");
        for (StackTraceElement stack_tra_ele: ste)
            System.out.println(stack_tra_ele);
    }
}

Output

E:\Programs>javac GetStackTrace.java

E:\Programs>java GetStackTrace
We are using for loop to display stack trace elements
java.lang.Thread.getStackTrace(Thread.java:1589)
GetStackTrace.main3(GetStackTrace.java:29)
GetStackTrace.main2(GetStackTrace.java:22)
GetStackTrace.main1(GetStackTrace.java:15)
GetStackTrace.main(GetStackTrace.java:7)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.