Problem with Unittest - Thanks:
Bengt Richter
bokr at oz.net
Wed May 14 23:03:08 EDT 2003
On Wed, 14 May 2003 14:26:52 -0400, "Terry Reedy" <tjreedy at udel.edu> wrote:
>
>"Frithiof Andreas Jensen" <frithiof.jensen at removethis.ted.ericsson.dk>
>wrote in message news:b9tn02$5ac$1 at newstree.wise.edt.ericsson.se...
>>
>> Is it legal, moral and commendable to raise exceptions like: 'raise
>> ValueError('some explanation text')?
>
>For the dubious: Ref Manual, 6.9 The raise statement
>"Otherwise, raise evaluates its first expression, which must yield a
>string, class, or instance object. .... If the first expression is an
>instance object, the second expression must be None." [or omitted,
>unless third expression is present]
>
>try:
> raise Exception("Test of instance raising")
>except Exception, haha:
> print haha, type(haha)
>
>#prints
>Test of instance raising <type 'instance'>
>
I'm not sure what you are demonstrating, other than that it's legal ;-) I.e.,
you would get the same haha instance if you raised Exception with a comma and the string.
Anyway, some additional things to notice:
>>> class Classic:
... def __init__(self, *args): self.args = args
... def __str__(self): return '<[Classic %r]>' % `self.args`
...
>>> class NewStyle(object):
... def __init__(self, *args): self.args = args
... def __str__(self): return '<[NewStyle %r]>' % `self.args`
...
>>> for x in [Classic, Classic(1), Classic(1,2,3), NewStyle, NewStyle(1,2,3)]:
... try:
... raise x
... except Classic, e:
... print '%s! : %s\n args: %r' % (e.__class__.__name__, e, e.args)
... except Exception, e:
... print '%s! : %s\n args: %r' % (e.__class__.__name__, e, e.args)
... except:
... print sys.exc_info()
...
Classic! : <[Classic '()']>
args: ()
Classic! : <[Classic '(1,)']>
args: (1,)
Classic! : <[Classic '(1, 2, 3)']>
args: (1, 2, 3)
TypeError! : exceptions must be strings, classes, or instances, not type
args: ('exceptions must be strings, classes, or instances, not type',)
TypeError! : exceptions must be strings, classes, or instances, not NewStyle
args: ('exceptions must be strings, classes, or instances, not NewStyle',)
Regards,
Bengt Richter
More information about the Python-list
mailing list