Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

AESP task 4a: Handle errors so nothing breaks

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

60 minutes

One bad cell stops the whole report

A report runs over 2000 rows. On row 1347 somebody typed "n/a" in an amount cell. The program stops with a wall of red text.

The owner sees nothing. She does not know which row, or what to do. Good error handling turns a crash into a clear message.

Key words

WordWhat it means
Floata number with a decimal point, such as 12.5
Exceptionan error that stops code unless you handle it
Handlecatch an exception and decide what happens next
Try and exceptcode that tries something and, if it fails, runs a rescue instead
Dictionarya collection of values, each found by a name called a key
Messagewords shown to a user that say what went wrong and what to do
Fail safewhen something goes wrong, nothing is harmed and nothing is made up

The cells students fill in are left blank in this preview.

By the end of this lesson

Note

I can catch common exceptions, give helpful messages and make a solution fail safely.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

This is practice

Important

The real task comes from the exam board. This lesson uses an invented organisation and its own made-up data. Your teacher tells you how the real tasks are set and what you may bring.

Run it: the crash

Code: python
amounts = ["12.50", "n/a", "7"]
total = 0
for text in amounts:
    total = total + float(text)
print(total)

What the crash tells you

The program stopped on "n/a" with a ValueError. It never printed a total, and it never said which value was wrong.

Now wrap the risky line in try and except. The code carries on and tells the user the useful part.

Run it: the same data, handled

Code: python
amounts = ["12.50", "n/a", "7"]
total = 0
skipped = []
for text in amounts:
    try:
        total = total + float(text)
    except ValueError:
        skipped.append(text)
print("Total", total)
print("Skipped", skipped)

Worked example: common exceptions

ExceptionCauseA helpful reaction
ValueErrorText that is not a number goes into float()Say what was expected, with an example
ZeroDivisionErrorDividing by zero, such as an average of nothingSay there is no data
KeyErrorAsking a dictionary for a key it does not holdSay the item is unknown, and name it
FileNotFoundErrorA file is missingSay which file, and where to put it

The cells students fill in are left blank in this preview.

The lesson goes on

7 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.