Deriving from Exception

Hans Nowak wurmy at earthlink.net
Thu Jul 11 00:05:01 EDT 2002


[/F]
>>If this behaviour is fine for your exception subclass, there's
>>no need to override anything.

[pixie888]
> class CMyException(Exception):
>    def __init__(self,ID):
>       self.ID = ID
> 
> I do NOT call the __init__ function of Exception in the __init__
> function of CMyException: how does that relate to the explanation you
> gave above? Will those args (what are they anyway, is args a reserved
> word?) be copied then? I tought that *not* calling the __init__ of
> Exception in the __init__ of CMyException resulted in not running the
> code provided in that base __init__ function. Did I miss something
> here?

Sort of: the point is that in normal cases, you don't have to write your own 
__init__ here. For example:

class CMyException(Exception): pass

try:
     raise CMyException("42")
except CMyException, e:
     print e.args
	
# prints ('42',)

What you pass to the exception can be retrieved using the args attribute. Of 
course, your situation is a bit different, you store the ID as an attribute of 
the exception object... is there a special reason why you want to do that?

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/




More information about the Python-list mailing list