How to Read a Stack Trace in Python, Java and JavaScript
A practical guide to reading tracebacks and stack traces — which end to start from in each language, how to find the first line of your own code, and how to decode chained exceptions, "Caused by" and minified browser errors.
By Software Side Hustles Editorial Team · Published · Updated · 4 min read
Quick answer: a stack trace is the list of function calls that were active when an error happened. Find three things: the exception type and message (what went wrong), the frame where it was raised (where), and the first frame in your own code (where you can fix it). In Python the most recent call is at the bottom; in Java and JavaScript it is at the top.
Anatomy of a stack trace
Every stack trace contains the same information:
- The exception type —
TypeError,NullPointerException,KeyError. - The message — often the most useful line:
'NoneType' object is not subscriptable,Index 5 out of bounds for length 5. - A list of frames, each with a file, line number and function name.
The frames read like a breadcrumb trail: main called process_order, which called calculate_tax, which raised the error.
Reading a Python traceback
Traceback (most recent call last): File "/app/main.py", line 14, in <module> report = build_report(orders) File "/app/reports.py", line 22, in build_report totals = [order_total(o) for o in orders] File "/app/reports.py", line 9, in order_total return sum(item["price"] * item["qty"] for item in order["items"]) ~~~~~^^^^^^^^^TypeError: 'NoneType' object is not subscriptable- "most recent call last" means read from the bottom up.
- The last line is the error: something was
None. - The frame just above it is where it happened:
reports.py, line 9, inorder_total. - The
~~~~~^^^^^^^^^markers (Python 3.11+) point at the exact sub-expression:order["items"]— soorderwasNone. - Moving up shows who passed that order in:
build_reportcalled frommain.py.
Chained exceptions in Python
KeyError: 'DATABASE_URL'
During handling of the above exception, another exception occurred:...RuntimeError: Configuration incomplete"During handling of the above exception" means a second error occurred inside an except block. "The above exception was the direct cause of the following exception" means code used raise NewError(...) from original. In both cases the first exception printed is usually the root cause.
Reading a Java stack trace
Exception in thread "main" java.lang.IllegalStateException: Could not load orders at com.shop.OrderService.load(OrderService.java:41) at com.shop.ReportJob.run(ReportJob.java:18) at com.shop.Main.main(Main.java:9)Caused by: java.sql.SQLException: Connection refused at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:342) ... 12 more- Java prints the most recent call first, so read from the top down.
- The first line is the exception and message.
Caused by:sections are chained causes. The lastCaused byis usually the root cause — here, the database refused the connection. TheIllegalStateExceptionwas just a wrapper.... 12 moremeans "the remaining frames are the same as in the exception above" — Java trims duplicated frames.
In frameworks like Spring, traces can be hundreds of lines long, mostly proxy and framework frames (org.springframework..., jdk.internal.reflect...). Skim for your own package name (com.shop) to find the relevant lines.
Reading a JavaScript stack trace
TypeError: Cannot read properties of undefined (reading 'total') at sumOrders (/app/src/report.js:12:27) at buildReport (/app/src/report.js:30:15) at async main (/app/src/index.js:8:3)- The top frame is the most recent:
report.js, line 12, column 27. async mainshows the async caller — Node.js and modern browsers reconstruct "async stack traces" acrossawait.- Frames from
node_modulesornode:internalare library and runtime code; look for the first frame from yoursrcfolder.
Minified production errors
In the browser, production errors look like at e (main.4f9c2b.js:1:20334). Upload source maps to your error-tracking tool (Sentry, Datadog, Rollbar) or enable them in DevTools; they map minified positions back to your original files and lines.
How to use the stack trace to find the bug
- Read the message twice. It frequently tells you the answer: a key name, an index and length, a variable that was null.
- Find the first frame in your code. Library code is rarely wrong; your call into it usually is.
- Open that line and ask what value would produce this error.
'NoneType' object is not subscriptableonorder["items"]meansorderisNone. - Move one frame outwards to see where that value came from. Repeat until you reach the place where the value became wrong.
- Reproduce with a breakpoint or a print statement just before the failing line, then fix and add a test. See how to debug code systematically.
Getting better stack traces
- Do not swallow exceptions.
except Exception: passand emptycatchblocks delete the stack trace you need. - Chain when you wrap. Python:
raise ConfigError("...") from e. Java:throw new ServiceException("...", e). JavaScript:throw new Error("...", { cause: err }). Wrapping without the cause loses the root of the problem. - Log the exception object, not just its message.
logger.exception("Failed")in Python andlog.error("Failed", e)in Java include the full trace; loggingstr(e)does not. - Keep debug information in builds (Java
-g, source maps in JavaScript) so traces show names and lines.
Frequently asked questions
Why is the line number in the stack trace wrong?
Usually the file changed after the program started (restart it), the code was transpiled without source maps, or a multi-line expression is reported at its first or last line.
What does <module> mean in a Python traceback?
Code running at the top level of a file, not inside any function.
What is the difference between a stack trace and a traceback?
Nothing important — Python calls it a traceback; most other languages call it a stack trace.
Where this skill pays off
Reading a trace quickly and naming the real cause is central to judging an AI model's debugging answers — does its fix address the root cause, or just the symptom? That judgement is paid work; see how to evaluate AI-generated code and the free practice labs.