raising classes

Greg Ewing see_reply_address at something.invalid
Mon Aug 19 23:55:24 EDT 2002


Paul Rubin wrote:
 >

> Are you saying the 'except' statement treats class objects specially,
> and figures out if a thrown class object is a subclass of the caught
> class, while using something like isinstance if you throw an instance
> rather than a class object?


The code which does the "raise" first makes sure the
exception is instantiated (which involves testing whether
it's a class or instance, and then extracts the class from the
instance and stores the exception as a triple
(class, instance, traceback).

When searching for an exception handler, it uses the
"class" field of the exception triple, using issubclass
to test whether it matches the classes in the "except"
clauses.

So yes, different things are done depending on whether
you raise a class or an instance (or string, for backwards
compatibility), but (in interpreted code at least) the
difference occurs in the "raise" statement rather than
the "except" statement.

Incidentally, I think this is one of the reason why exceptions
currently must be old-style classes. New-style classes
are also new-style instances, so the "is this an instance"
test wouldn't make sense.

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg




More information about the Python-list mailing list