Reading a stack trace: Evaluating error messages correctly
In short: A stack trace lists the call chain up to the error location; if you read it from the right side and watch your own code, you often find the cause in minutes.
In more detail: When a program crashes it prints an error message with many lines. Many beginners are startled, yet the stack trace is a route description to the error: it shows which function called which until it blew up.
In Depth
Structure (Python)
Traceback (most recent call last):
File "main.py", line 12, in <module>
result = compute(data)
File "main.py", line 7, in compute
return total / count
ZeroDivisionError: division by zero
Python prints the oldest spot at the top, the last at the bottom. The last line names the error type and message. The line directly above shows where it happened (file, line number, function, code).
Other languages
- Java: at the top the exception (
NullPointerException), below it the calls, topmost the error location;Caused by:points to the original cause. - JavaScript/Node: error type and message first, then
at function (file:line:column), the first line is the error location. - Browser console: click the file link to see the spot in the source.
How to proceed
- Read error type and message and put it in your own words (“division by zero”).
- Look for the first line with your own code (not library/framework lines): that is where to go.
- Check variable values at that spot (
print, debugger, breakpoint). - Ask: where does the wrong value come from? Look one level up in the stack trace to see who calls the function with this data.
- Google the error message (in quotes, without your own names/paths) or look in the wiki/error dictionary.
- Build a minimal example that shows the error.
Asking in a forum
Post the complete stack trace as text (not as a photo), the relevant code lines, what you expected and what you already tried, versions (language, packages, system). Remove passwords, keys and personal data from logs. In production stack traces belong in the log, not in the response to users (they reveal internal details).
See also: Debugging, Exceptions, Logging and log level, Error codes overview