Week 61 — What is a stack trace and how can one read it?

Question of the Week #61
What is a stack trace and how can one read it?
6 Replies
Eric McIntyre
Eric McIntyre10mo ago
When an exception is created, the JVM records a stack trace. In case the exception isn't caught, the stack trace is typically printed to the console. For example, the following code throws an exception and never catches it:
package io.github.danthe1st.playground.stacktrace;

public class StackTraceDemo {
public static void main(String[] args) {
someMethod();
}

private static void someMethod() {
throw new IllegalStateException("This is for demonstration");
}
}
package io.github.danthe1st.playground.stacktrace;

public class StackTraceDemo {
public static void main(String[] args) {
someMethod();
}

private static void someMethod() {
throw new IllegalStateException("This is for demonstration");
}
}
When executing that class, a stack trace is printed to the console:
Exception in thread "main" java.lang.IllegalStateException: This is for demonstration
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.someMethod(StackTraceDemo.java:9)
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.main(StackTraceDemo.java:5)
Exception in thread "main" java.lang.IllegalStateException: This is for demonstration
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.someMethod(StackTraceDemo.java:9)
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.main(StackTraceDemo.java:5)
This stack trace gives information about the thrown exception and where it occurred. In the first part, it indicates the thread name the exception was thrown in (main). After that, the fully qualified name of the exception is shown. Since we threw an IllegalStateException, this is java.lang.IllegalStateException. At the end of the first line of the stack trace, it shows the message of the exception. As we passed the string This is for demonstration to the constructor of the exception, this is printed here. The lines after that provide information about where exactly the exception occured. The line at io.github.danthe1st.playground.stacktrace.StackTraceDemo.someMethod(StackTraceDemo.java:9) explains that it originated from the method someMethod of the class StackTraceDemo in the package io.github.danthe1st.playground.stacktrace. It also includes the name of the source file and the line number in that file corresponding to the source code the exception was created in (file StackTraceDemo.java, line 9). This stack trace gives information about the thrown exception and where it occurred. In the first part, it indicates the thread name the exception was thrown in (main). After that, the fully qualified name of the exception is shown. Since we threw an IllegalStateException, this is java.lang.IllegalStateException. At the end of the first line of the stack trace, it shows the message of the exception. As we passed the string This is for demonstration to the constructor of the exception, this is printed here. The lines after that provide information about where exactly the exception occured. The line at io.github.danthe1st.playground.stacktrace.StackTraceDemo.someMethod(StackTraceDemo.java:9) explains that it originated from the method someMethod of the class StackTraceDemo in the package io.github.danthe1st.playground.stacktrace. It also includes the name of the source file and the line number in that file corresponding to the source code the exception was created in (file StackTraceDemo.java, line 9). Further lines explain how the program got to that method. In this example, someMethod was called by main so there is another line indicating the main method with the same information.
Eric McIntyre
Eric McIntyre10mo ago
In many cases, exceptions are caused by other exceptions. This is typically represented by passing the "cause" to the constructor of the new exception:
package io.github.danthe1st.playground.stacktrace;

public class StackTraceDemo {
public static void main(String[] args) {
someMethod();
}

private static void someMethod() {
try{
otherMethod();
}catch(Exception e){
throw new RuntimeException(e);
}
}

private static void otherMethod() {
throw new IllegalStateException("This is for demonstration");
}
}
package io.github.danthe1st.playground.stacktrace;

public class StackTraceDemo {
public static void main(String[] args) {
someMethod();
}

private static void someMethod() {
try{
otherMethod();
}catch(Exception e){
throw new RuntimeException(e);
}
}

private static void otherMethod() {
throw new IllegalStateException("This is for demonstration");
}
}
This code would result in the following stack trace:
Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalStateException: This is for demonstration
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.someMethod(StackTraceDemo.java:12)
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.main(StackTraceDemo.java:5)
Caused by: java.lang.IllegalStateException: This is for demonstration
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.otherMethod(StackTraceDemo.java:17)
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.someMethod(StackTraceDemo.java:10)
... 1 more
Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalStateException: This is for demonstration
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.someMethod(StackTraceDemo.java:12)
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.main(StackTraceDemo.java:5)
Caused by: java.lang.IllegalStateException: This is for demonstration
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.otherMethod(StackTraceDemo.java:17)
at io.github.danthe1st.playground.stacktrace.StackTraceDemo.someMethod(StackTraceDemo.java:10)
... 1 more
Here, we see that a RuntimeException is thrown in StackTraceDemo.someMethod (the throw new RuntimeException(e); line). However, it also includes information about the other exception that occured. Specifically, the message is java.lang.IllegalStateException: This is for demonstration indicating that it was caused by an IllegalStateException. We can even see a Caused by section containing the stack trace of the IllegalStateException.
📖 Sample answer from dan1st
Eric McIntyre
Eric McIntyre10mo ago
A stack trace is a bunch of trace that appears when an error occurs in your code. It describes the path that led to the bug within your project files. It's very useful for understanding the nature of the bug and how it occurred. It can be read in a console and might be in a log file if configured.
Submission from hirokx
Eric McIntyre
Eric McIntyre10mo ago
stack trace is a path of identifying the method in which the exception has occured
Eric McIntyre
Eric McIntyre10mo ago
the exception method can be identified by creating Expection class object and by placing call to printStackTrace() method.
Submission from chandru9092
Eric McIntyre
Eric McIntyre10mo ago
A stack trace is a report that shows the sequence of method calls leading up to an error in a Java program. It's printed when an exception occurs. Each line in the stack trace represents a method call, starting from the method where the exception was thrown and going back through the call stack. Here's an example along with a stack trace:
public class StackTraceExample {
public static void main(String[] args) {
try {
method1();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void method1() {
method2();
}

public static void method2() {
int a = 5;
int b = 0;
int result = a / b; // This line will cause an ArithmeticException
}
}
public class StackTraceExample {
public static void main(String[] args) {
try {
method1();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void method1() {
method2();
}

public static void method2() {
int a = 5;
int b = 0;
int result = a / b; // This line will cause an ArithmeticException
}
}
When you run this code, you'll get a stack trace showing an ArithmeticException caused by division by zero. It'll indicate which line caused the error ( int result = a / b; ) and the method calls leading up to it ( method2() -> method1() -> main() )
⭐ Submission from ig.imanish
Want results from more Discord servers?
Add your server