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
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:
When executing that class, a stack trace is printed to the console:
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.In many cases, exceptions are caused by other exceptions. This is typically represented by passing the "cause" to the constructor of the new exception:
This code would result in the following stack trace:
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
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
stack trace is a path of identifying the method in which the exception has occured
the exception method can be identified by creating Expection class object and by placing call to printStackTrace() method.
Submission from chandru9092
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: When you run this code, you'll get a stack trace showing anArithmeticException
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