replacing built-in exception types

N

Nishkar Grover

I'm trying to replace a built-in exception type and here's a simplified
example of what I was hoping to do...
.... bar = 'bar'
........ raise ZeroDivisionError
.... except ZeroDivisionError, e:
.... print e.bar
....
bar.... 1/0
.... except ZeroDivisionError, e:
.... print e.bar
....
Traceback (most recent call last):

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
 
D

Dennis Lee Bieber

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?
Doubt it is possible... "1/0" is likely done at the core {C code for
common Python level}, and that core already has references to the base
exception. Your "override" only rebinds the name in the module, but
wouldn't affect anything that has previously locked the name..
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Stargaming

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

I don't know why you're fiddling that much with the names,
`ZeroDivisionError` is a builtin name. If you do this to avoid
overwriting the base classes, consider the following example::
True

The names really just point to an object. Once the resolution from a name
to a real object is done, you can reuse the name.
... bar = 'bar'
...
... raise ZeroDivisionError
... except ZeroDivisionError, e:
... print e.bar
...
bar
... 1/0
... except ZeroDivisionError, e:
... print e.bar
...
Traceback (most recent call last):

The object that ZeroDivisionError points to in your code is *not* the
raised exception.
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?

The obvious question is: What are you trying to do?

Remember that you can always catch an exception in an `except` clause and
perform whatever action you want, eg. raise *another* exception.

If you just want to change error representations (I doubt that you really
need it, tho), you might be interested in overwriting `sys.excepthook
<http://docs.python.org/lib/module-sys.html#l2h-5125>`_.

HTH,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top