Skip to contentSkip to navigation

Module 3 of 6, Practical Python

Errors and Exceptions

Read error messages, recover from failures, and raise errors of your own.

module checksSign in to track

  • Lessons complete-, not done
  • Lesson quizzes taken-, not done
  • Module Labs passed-, not done
  • Module exam passed at 80%-, not done
lessons
3
quiz questions
36
reading time
25 min
exam length
15 Q
coding exercises
5

Lessons

  1. Errors and ExceptionsRead Python's error messages with confidence: tell syntax errors from runtime exceptions, read a traceback from the bottom up, and recognise the common exception types and their causes. 9 min 12 questions
  2. Handling Exceptions with try and exceptKeep a program running when something goes wrong: catch specific exceptions with try and except, read the error with as, and use else, finally and raise in the right places. 8 min 12 questions
  3. Raising and Defining Your Own ExceptionsMake your functions report problems clearly: raise built-in exceptions with good messages, define your own exception classes, group them in a small family, and chain them with raise from. 8 min 12 questions

Lab: write it yourself

Real Python, real checks: write your answer, press Run checks, and keep going until it passes. (2 exercises)

All module exercises

The Lab needs an account. Sign in or create a free ID to run these exercises.

Lab 1: Divide without crashing

Sign-in required

Divide 900 by people = 0 inside try/except so that instead of a crash it prints People must be more than zero.

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

Lab 2: Parse a number safely

Sign-in required

The text "abc" should become a number. Convert it inside try/except ValueError; on failure print not a number.

Sign in to practise

Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.

References & Further Reading

Related entries from PyLabs's own reference library.

  • open() (Open a file so you can read from it or write to it.)
  • isinstance() (Check whether a value is an instance of a type.)
  • iter() (Return an iterator for a collection or other iterable.)