JavaHow to Use StackTraceElement in Java

How to Use StackTraceElement in Java content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Using StackTraceElement, Java developers can know the stack trace at any given point in the code flow. Below is a code example showing how to use StackTraceElement in Java:


public class UsingStackTraceElement
	public static void main(String args[])
		UsingStackTraceElement usingStackTraceElement = new UsingStackTraceElement();
	public void proceed()
	private void myMethod()
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
		for(StackTraceElement st : stackTrace)  


If you run this code in your integrated development environment (IDE) or code editor, you get the following output:

[root@mypc]# java UsingStackTraceElement
java.lang.Thread.getStackTrace(Unknown Source)

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories