[Tutor] Class as Exception

Magnus Lycka magnus@thinkware.se
Sun Feb 9 12:48:02 2003


Bob Gailer wrote:
>A couple of times I got the following to work. Now I just get an exception.
>
>  >>> class n(str):pass
>  >>> raise n, 'tst'
>Traceback (most recent call last):
>    File "<interactive input>", line 1, in ?
>TypeError: exceptions must be strings, classes, or instances, not type
>
>Why would the same code work, then fail?

I don't know, but if you want to create an exception
class, it should be an Exception, not a string. Try
doing

class MyError(Exception): pass

instead of subclassing string. Even if you would
get it to work in this particular case, you'd just
cause a lot of trouble.

See
http://www.python.org/doc/current/tut/node10.html
and
http://www.python.org/doc/current/lib/module-exceptions.html


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se