Python 5.4Code walkthrough8 parts

Exceptions

Handle errors deliberately instead of crashing.

Loading your progress…Next: 5.5 Debugging

In this lesson8 parts

  1. 01One bad row
  2. 02Reading a traceback
  3. 03try and except
  4. 04Asking until it is valid
  5. 05Several excepts, else and finally
  6. 06Raising your own
  7. 07Where exceptions mislead
  8. 08Recap, your challenge, and next

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

exception
An object Python raises when a line can not be carried out; unhandled, it stops the program.
handle
Catch an exception with except, so the program carries on.
raise
Create an exception and stop the function with it: raise ValueError(...).
traceback
The report Python prints for an unhandled exception: the calls that led to it, and the error itself on the last line.

Quiz 5 questions

Your first pick on each question is the one that counts, and a right one earns a coin. Getting one wrong here is how the lesson sticks.

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 2 · Files and Exceptions.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.