Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
So far, I’ve been deliberately vague about what an exception actually is. As suggested in the prior chapter, in Python 2.6 and 3.0 both built-in and user-defined exceptions are identified by class instance objects. Although this means you must use object-oriented programming to define new exceptions in your programs, classes and OOP in general offer a number of benefits.
Here are some of the advantages of class-based exceptions:
They can be organized into
categories. Exception classes support future changes by
providing categories—adding new exceptions in the future won’t
generally require changes in try
statements.
They have attached state
information. Exception classes provide a natural place
for us to store context information for use in the try handler—they may have both attached
state information and callable methods, accessible through
instances.