Caffeinated Coder
Book an appointment
Lesson 14 · Real programs

Exceptions

Some mistakes only show up while a program runs: dividing by zero, a missing file, text where a number should be. Python then raises an exception, an object that names what went wrong. Follow one through a seven-line program, share.py: it is raised inside a function, climbs out of the function's frame, and is caught by try and except so the program carries on. Then delete the try and watch Python print a traceback and stop.

12 min10 steps5 questionsIntermediate
Fig 14 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.