Exceptions & Testing

5. Exceptions & Testing#

Errors are inevitable in any program. This chapter teaches you to recover from runtime failures and find logic errors before they reach users.

  • Exception handling for runtime failures

  • Tracebacks as debugging evidence

  • Custom exceptions for clearer error reporting

  • Tests as executable checks of program behavior

Video

This overview introduces exception handling with try, except, else, and finally.

Learning Goals

By the end of this chapter, you will be able to:

  1. Distinguish between syntax errors, runtime errors, and semantic errors

  2. Read and interpret Python tracebacks to locate the source of a bug

  3. Handle exceptions gracefully with try, except, else, and finally

  4. Raise exceptions and define custom exception classes

  5. Write doctest, unittest, and pytest tests to verify behavior

Chapter Flow

Chapter Overview Slides