flowchart TD A["Traceback (most recent call last):"] --> B["outermost frame -- where the call chain began"] B --> C["... intermediate frames ..."] C --> D["innermost frame -- the line that raised"] D --> E["ExceptionType: message"] E --> F["1. read the type and message FIRST"] F --> G["2. find the lowest frame in YOUR code"] G --> H["3. walk up only if that frame looks correct"] I["'During handling ... another exception occurred'"] --> J["two tracebacks: the second one is the cause"] K["'The above exception was the direct cause'"] --> L["raise ... from -- read the FIRST block"]
A repeatable debugging process
When you see an exception:
- Read the last line (exception type + message)
- Go to the line number where it occurred
- Check the types/values of variables
- Create a minimal reproduction
- Fix and re-run
Example traceback
def avg(nums):
return sum(nums) / len(nums)
print(avg([]))Traceback (most recent call last):
File "bug.py", line 4, in <module>
print(avg([]))
File "bug.py", line 2, in avg
return sum(nums) / len(nums)
ZeroDivisionError: division by zeroFix approach
- The list is empty, so
len(nums)is 0. - Decide policy: return
None, raise a clear error, or handle with default.
def avg(nums):
if len(nums) == 0:
raise ValueError("avg() requires at least one number")
return sum(nums) / len(nums)
print(avg([10, 20]))Quick tip: print types
value = "10"
print(type(value), value)
print(int(value))Optional: use the debugger
Python has a built-in debugger.
import pdb
x = 10
pdb.set_trace()
print(x)Useful commands inside the debugger:
nnextsstep intop varprint variableccontinue
Check yourself
In a Python traceback, which line tells you where the exception was RAISED?
The header says 'most recent call last'. Frames are printed in call order, so the bottom frame is the innermost one.
Show answer
B — The last `File` line, just above the exception message — The header says 'most recent call last'. Frames are printed in call order, so the bottom frame is the innermost one.
A traceback runs through twenty frames of a web framework. What is the fastest way in?
Everything below your lowest frame is usually library code doing what it was told. The mistake is normally in your frame or in the arguments it passed.
Show answer
B — Read the exception line, then scan up for the lowest frame in your own code — Everything below your lowest frame is usually library code doing what it was told. The mistake is normally in your frame or in the arguments it passed.
Two tracebacks are printed with 'During handling of the above exception, another exception occurred' between them. What does that mean?
That phrasing means implicit chaining via `__context__`. 'The above exception was the direct cause' is the other one — deliberate chaining with `raise ... from`, where the first block is the story.
Show answer
B — The second happened while handling the first, and is often incidental — That phrasing means implicit chaining via `__context__`. 'The above exception was the direct cause' is the other one — deliberate chaining with `raise ... from`, where the first block is the story.
Why does `raise ... from None` not actually erase the original exception?
Verified: after `from None`, `__cause__` is None but `__context__` still holds the original. A logger walking the chain can still find it.
Show answer
B — It only suppresses the DISPLAY; `__context__` is still set — Verified: after `from None`, `__cause__` is None but `__context__` still holds the original. A logger walking the chain can still find it.
🧪 Try It Yourself
Exercise 1 – Read a Traceback
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Traceback Module
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Get Traceback as String
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.