replacing built-in exception types

Nishkar Grover Nishkar.Grover at pdi.dreamworks.com
Tue Dec 11 15:51:52 EST 2007


I'm trying to replace a built-in exception type and here's a simplified 
example of what I was hoping to do...

 >>>
 >>> import exceptions, __builtin__
 >>>
 >>> zeroDivisionError = exceptions.ZeroDivisionError
 >>>
 >>> class Foo(zeroDivisionError):
...     bar = 'bar'
...
 >>>
 >>> exceptions.ZeroDivisionError = Foo
 >>> ZeroDivisionError = Foo
 >>> __builtin__.ZeroDivisionError = Foo
 >>>
 >>> try:
...     raise ZeroDivisionError
... except ZeroDivisionError, e:
...     print e.bar
...
bar
 >>>
 >>> try:
...     1/0
... except ZeroDivisionError, e:
...     print e.bar
...
Traceback (most recent call last):
   File "<stdin>", line 2, in ?
ZeroDivisionError: integer division or modulo by zero
 >>>

Notice that I get my customized exception type when I explicitly raise 
ZeroDivisionError but not when that is implicitly raised by 1/0. It 
seems like I have to replace that exception type at some lower level, 
but I'm not sure how/where. Does anyone know of a way to do this?

- Nishkar




More information about the Python-list mailing list