PyCon 2016 in Portland, Or
hills next to breadcrumb illustration

Tuesday 12:10 p.m.–12:55 p.m.

Exception and error handling in Python 2 and Python 3

Alex Martelli

Audience level:
Novice
Category:
Best Practices & Patterns

Description

Handling errors and exceptions optimally is crucial in solid Python programs. Some technical details have changed in Python 3, and the talk covers those, but the core of "best practices" is quite enduring, and the talk focuses on presenting and explaining them.

Abstract

Python uses exceptions to indicate essentially all errors, and a few non-error conditions such as an iterator finishing. Mastering the details on how to raise and handle exceptions (including generators and context managers) is thus a crucial step towards Python mastery, and this talk covers those details, including the changes between Python 2 and Python 3. Even more important, however, are the **best practices** for dealing with errors and other exceptions, especially in a program which must catch (and log and/or report) exceptions, but keep running afterwards, such as a typical "server" program. This is the main focus of the talk, covering "Look Before You Leap" and "Easier to Ask Forgiveness than Permission" patterns, avoiding `assert`, keeping your exception-catching as narrow as possible, and how to properly arrange for the right kind of error-reporting to an application's user (when excessive detail would be damaging) **and** to maintainers to identify (and reproduce, and fix) bugs in your code.