how to get a return value from an exception ?

Chris Liechti cliechti at gmx.net
Thu Jul 25 10:23:07 EDT 2002


"Shagshag13" <shagshag13 at yahoo.fr> wrote in
news:ahp0n4$upavu$1 at ID-146704.news.dfncis.de: 
> i need to raise an exception which "carry" some data... i think that
> it's something like : 
...
> 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`

thats fine. maybe you want to pass a string and other args in addition:
no need to overload __str__ in this case (unless u want to have the 
"value" in the message):

class MyError(Exception):
    	def __init__(self, message, value):
    	    	Exception.__init__(self, msg) #call supperclass' init
        	self.value = value
 
>>> try:
... 	raise MyError("hello",5)
... except MyError, e:
... 	print e, e.value
... 	
hello 5

> but here what does the `` stand for ?

"repr()"

i think that's a bad shortcut for it, however some people seem to like it 
:-(

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list