Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Besides supporting flexible hierarchies, exception classes also
provide storage for extra state information as instance attributes. As
we saw earlier, built-in exception superclasses provide a default
constructor that automatically saves constructor arguments in an
instance tuple attribute named args. Although the default constructor is
adequate for many cases, for more custom needs we can provide a
constructor of our own. In addition, classes may define methods for
use in handlers that provide precoded exception processing
logic.
When an exception is raised, it may cross arbitrary file
boundaries—the raise statement
that triggers an exception and the try statement that catches it may be in
completely different module files. It is not generally feasible to
store extra details in global variables because the try statement might not know which file
the globals reside in. Passing extra state information along in the
exception itself allows the try
statement to access it more reliably.