# Exceptions and Debugging

> What an exception is, and how to catch one with try and except. How to read a stack trace from the bottom, and how to hunt down a bug.

**Course:** [Programming Basics](https://learn.benibix.com/learn/development-concepts)  
**Time:** 14 min

**After this lesson you can:**

- Catch an exception with try and except
- Read a stack trace to find the failing line
- Narrow down a bug with print and a debugger

## How to read an error report and find the bug

An exception is the signal Python raises when something breaks while the program runs. Each kind has a name that says what went wrong, such as KeyError for a missing key. If nothing deals with it, the program stops and prints a report. Python has dozens of these kinds built in.

You can catch an exception so the program does not crash. Put the risky line inside try:, and the plan B inside except ValueError:. If the line works, the except block is skipped. If it raises that error, Python jumps straight to the except block and carries on from there.

Catch only the error you expect, and only where you can do something useful about it. A bare except: catches everything, including bugs you needed to see. A wrong number typed by a user is worth catching. A typo in your own code is not.

When an exception is not caught, Python prints a stack trace. It starts with Traceback (most recent call last): and lists every function call that led to the failing line. Read it from the bottom. The last line names the error, and the line just above it shows where it happened.

Debugging is the work of finding why code goes wrong, then fixing it. Start by making the bug happen again, every time. Then narrow down where it lives. The quickest tool is a print line that shows a value, placed halfway through. If the value is already wrong there, the bug is above it.

A debugger does the same job with more control. It pauses the program at a line you choose, called a breakpoint. Then you can step through one line at a time and see every variable as it changes. Python ships with one, called pdb, and most code editors build one in.

Some bugs fall to explaining the code out loud, line by line, to anyone, even a rubber duck. Saying what each line really does shows the gap between that and what you thought it did. The common mistake is guessing and changing lines at random. Narrow it down first.

```python
text = input("How many people? ")
try:
    people = int(text)
    print("Each pays", 60 / people)
except ValueError:
    print("Please type a whole number.")
except ZeroDivisionError:
    print("There must be at least 1 person.")
```
Two things can go wrong here, and each gets its own except block. Any other error still shows up.

![Two panels. Print debugging: lines of code with print lines showing price and total, and the output DEBUG price: 0 marked first wrong value. Rubber duck debugging: three numbered steps said out loud, with the third, stop before length minus 1, marked skips one.](https://learn.benibix.com/diagrams/left1-debugging.svg)

### Example: Jack reads his first stack trace

Jack's app crashes and prints 9 lines. He reads from the top and is lost in function names he never wrote.

Then he reads from the bottom. The last line says KeyError: 'price', and the line above points to line 42 of cart.py. One item in the data had no price, and he fixes it there.

**Words in this lesson:** [Exception](https://learn.benibix.com/term/development/exception), [Stack Trace](https://learn.benibix.com/term/development/stack-trace), [Debugging](https://learn.benibix.com/term/development/debugging)

**Question:** Where should you start reading a Python stack trace?

**Answer:** At the bottom, where the error is named. The last line names the error, and the line above it shows where it happened. The lines higher up show the calls that led there.

---

Source: https://learn.benibix.com/learn/development-concepts/exceptions-and-debugging
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
