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:
- 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
- Principles of solution oriented error handling
- responsibilities between user, admin and developer
- when to use raise or assert
- 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
- 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
- Template for a solution oriented command line application
- Best practices for raise and except
- When to use raise?
- When to use except?