Reliable code

Handling problems you expect, reporting problems clearly, proving that code works, and finding bugs calmly.

About 7 hours of study

Learning objectives

Lessons

  1. 1

    Errors and exceptions

    Read error messages, and handle expected problems with try and except.

  2. 2

    Raising your own errors

    Signal problems with raise, define your own exception classes, and clean up with finally.

  3. 3

    Testing your code

    Check that functions work with assert, think about edge cases, and read a failing test.

  4. 4

    Debugging

    Find bugs systematically: reproduce, watch the values, form a guess, and test it.

Module practice

Module quiz

1. Why is a bare except: usually a bad idea?
2. Which error does int("abc") raise?
3. When should your function raise an error?
4. Which input is the most useful edge case for a function that averages a list?
5. What is the first step in debugging?
6. Why compare floats with abs(a - b) < 1e-9?

‹ Working with dataObjects and classes ›