Contribute Media
A thank you to everyone who makes this possible: Read More

Solution oriented error handling


This talk shows how to use Python's built in error handling mechanisms to keep the productive code clean, derive error messages helpful for the user directly from the code and release ressources properly.


Traditionally error handling is regarded an annoyance by developers because it removes the focus from the already difficult enough productive parts of the code to parts that ideally will never be called. And even if, end users seem to be ignore the error messages and just click "Ok" or call the help desk.

Solution oriented error handling uses Python's existing try/catch/finally idiom, with statement, assert statement and exception hierarchy in a way that keeps the code clean and easy to maintain. It gives a clear distinction between errors that can be solved by the end user, the system administrator and the developer. Naming conventions and a simple set of coding guidelines ensure that helpful error messages can be easily derived from the code.

Most code examples work with Python 2.6+ and Python 3.x, on a few occasions minor differences are pointed out.

Topics covered are:

  1. Introduction to error handling in Python
    • What are errors?
    • How to represent errors in Python
    • Detecting errors
    • Delegating errors to the caller
    • clean resource management
  2. Principles of solution oriented error handling
    • responsibilities between user, admin and developer
    • when to use raise or assert
  3. Error messages
    • What are "good" error messages
    • How to derive error messages from the source code
    • Adding context to the error
    • How to report errors to the user
  4. Solution oriented usage of Python's exception hierarchy
    • admins fix EnvironmentError
    • users fix DataError
      • representing DataError
      • converting exceptions to DataError
    • developers fix everything else
    • special Python exceptions not representing errors
  5. Template for a solution oriented command line application
  6. Best practices for raise and except
    • When to use raise?
    • When to use except?

This talk is a translation of a German talk given at the PyGRAZ user group and in a (slightly depythonized variant) the Grazer Linux Tag 2013 (slides and video).

Improve this page