[Tutor] Class as Exception

Gonçalo Rodrigues op73418@mail.telepac.pt
Sun Feb 9 07:36:31 2003


----- Original Message -----
From: "Bob Gailer" <ramrom@earthling.net>
To: <tutor@python.org>
Sent: Saturday, February 08, 2003 10:30 PM
Subject: [Tutor] Class as Exception


> 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?
>

To amplify on S. Perry answer, the line

raise n, tst

is effectively raise n(tst), that is, if you provide args to the exception,
it must be an instance. If no args are given, the class (derived from
Exception) is enough.

> Bob Gailer
> mailto:ramrom@earthling.net
> 303 442 2625

All the best,
G. Rodrigues