R: [Python-Dev] Deprecating string exceptions

Martin v. Loewis martin@v.loewis.de
28 Mar 2002 00:18:29 +0100


"Samuele Pedroni" <pedroni@inf.ethz.ch> writes:

> But this is legal in 2.2
> 
> >>> class Z(str,Exception):
> ...  pass
> ...
> >>> Z.__bases__
> (<type 'str'>, <class exceptions.Exception at 0x00757700>)
> >>> Z.__base__
> <type 'str'>
> 
> so the issue is more subtle or I'm missing something?

The issue is more subtle: raising an instance of Z counts as raising a
string, so you cannot catch with with "except Z", but you can catch
it with the identical object.

However, this is not the issue reported in bug #518846: Magnus Heino
does not want to inherit from str, and his class does not inherit from
Exception, either.

Also, I believe that using your class Z as an exception class should
not be supported: a "proper" exception should not just merely inherit
from Exception - it should have Exception as its only root class (Z
has both object and Exception as root classes).

So yes, funny things can happen, but they are all accidental, and may
stop happening without notice.

Regards,
Martin