liveBook · Manning

Stack trace

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A stack trace is a chronological report of the call path and source locations associated with an exception or other execution event.

A stack trace records the call path that led to an exception, showing each function, its source line, and the line where execution failed. It appears when no handler intercepts the exception and the application exits. section 5.1

Read a stack trace from the outer entry point toward the failure:

  • Start at the root of the application and follow the listed calls downward. section 5.1
  • Use each function name and line number to reconstruct the execution path. section 5.1
  • Treat the final location and exception message as the immediate failure point. section 5.1
  • Log the stack trace when diagnosing failures, and avoid handlers that silently discard it. section 5.2.2 section 5.2.4

Following a division failure

func_a() calls func_b(), which calls func_c(). When func_c() divides 12.2 by zero, the trace leads back through func_b() and func_a(), ending with ZeroDivisionError: float division by zero. section 5.1

Enter a dividend value: 12.2
Enter a divisor value: 0
Traceback (most recent call last):
  File "<path to code>/code/project/CH_05/example_01.py",
   line 20, in <module>
    func_a()
  File "<path to code>/code/project/CH_05/example_01.py",
   line 8, in func_a
    result = func_b(dividend, divisor)
  File "<path to code>/code/project/CH_05/example_01.py",
   line 13, in func_b
    return func_c(dividend, divisor)
  File "<path to code>/code/project/CH_05/example_01.py",
   line 17, in func_c
    return dividend / divisor
ZeroDivisionError: float division by zero

Inspect a complete trace with nested function calls, source lines, user input, and the final division error. section 5.1

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Defines stack traces and demonstrates nested calls, but stops short of internals beyond call-stack traversal and source locations.
Examples
Uses division by zero, invalid numeric input, nested functions, and logged exception output.
What it assumes
Assumes familiarity with functions and exceptions while demonstrating call-stack traversal from a concrete program.
What you can do after
Diagnose/debug with it by tracing failures to source lines and preserving exception details in logs.
How it connects
Connects exceptions with call stacks, exception handlers, logging, and debugging.

Also explained in

Compare how 7 books explain stack trace →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.