Failing on string exceptions in 2.4

Fredrik Lundh fredrik at
Fri Jun 15 08:56:52 CEST 2007

Stephen R Laniel wrote:

> End goal being that string exceptions would cause
> compilation to fail. A few times now, I've found myself
> doing
> class SomeClass:
>     """docstring"""
>     pass
> raise SomeClass, "Some description"
> and I've gotten a weird compiler error about the constructor
> for SomeClass.

weird as in

    TypeError: this constructor takes no arguments


this has nothing to do with string exceptions (using string objects as 
exception objects), and all to do with the fact that

     raise SomeClass, "Some description"

is equivalent to

     raise SomeClass("Some description")

which of course doesn't work if your class constructor isn't accepting 
any arguments.  this is no different from doing:

     class SomeClass:

     obj = SomeClass("an argument") # fails with a TypeError

(note that it's the runtime that's complaining, not the compiler)

to fix this, just make sure that your exception class takes an argument 
(the exception value), either by adding an __init__ method, or by 
inheriting from a suitable built-in exception class.


More information about the Python-list mailing list