subclassing Exceptions
phawkins at spamnotconnact.com
phawkins at spamnotconnact.com
Sun Jul 22 23:27:28 EDT 2001
Here's some exception behaviour I don't get. If I simply define an
exception class and use "pass" for the body, I can pass in arguments
when I raise it (see passException, below). If, like Sheila, I define
__str__, I can no longer pass in arguments -- even if I define __str__
with an argument -- see strException, and strExceptionWithArg.
Do I need to use variable args? No, that doesn't do the trick... see
varArgsStrException below.
>>> class strException(Exception):
def __str__(self):
return "string cheese?"
>>> raise strException
Traceback (innermost last):
File "<pyshell#4>", line 1, in ?
raise strException
strException: string cheese?
>>> class strExceptionWithArg(Exception):
def __str__(self,m):
return "string cheese? " + m
>>> raise strExceptionWithArg, "no thanks"
Traceback (innermost last):
File "<pyshell#7>", line 1, in ?
raise strExceptionWithArg, "no thanks"
strExceptionWithArg: <unprintable instance object>
>>> class passException(Exception):
pass
>>> raise passException
Traceback (innermost last):
File "<pyshell#11>", line 1, in ?
raise passException
passException:
>>> raise passException, "no thanks"
Traceback (innermost last):
File "<pyshell#12>", line 1, in ?
raise passException, "no thanks"
passException: no thanks
>>> class varArgsStrException(Exception):
def __str__(self, *args):
return "string cheese? " + str(args)
>>> raise varArgsStrException, "no thanks"
Traceback (innermost last):
File "<pyshell#18>", line 1, in ?
raise varArgsStrException, "no thanks"
varArgsStrException: string cheese? ()
More information about the Python-list
mailing list