how to get a return value from an exception ?
Alex Martelli
aleax at aleax.it
Thu Jul 25 10:20:05 EDT 2002
Shagshag13 wrote:
> hello,
>
> i need to raise an exception which "carry" some data... i think that it's
> something like :
>
> class MyException(Exception):
>
> def __init__(self, *args):
> Exception.__init__(self, args)
This method __init__ is useless. Just write:
class MyException(Exception): pass
for clarity and brevity.
>>>> raise MyException('one', 2, 'three')
> Traceback (most recent call last):
> File "<pyshell#66>", line 1, in ?
> raise MyException('one', 2, 'three')
> MyException: ('one', 2, 'three')
>
> but them how to access to 'one', 2, and 'three' ?
try: raise MyException('one', 2, 'three')
except MyException, e:
print repr(e.args)
e.args is the tuple ('one', 2, 'three') .
> it's ok if i do it like in "dive in python"
>
>>>> class MyError(Exception):
> def __init__(self, value):
> self.value = value
> def __str__(self):
> return `self.value`
>
> but here what does the `` stand for ?
A weird ancient way of spelling repr(self.value)
that some Pythonistas like (me, I don't).
Alex
More information about the Python-list
mailing list