473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

replacing built-in exception types


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

Dec 12 '07 #1
1 1396
On Tue, 11 Dec 2007 12:51:52 -0800, Nishkar Grover wrote:
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
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::
>>class A(object): pass
...
>>class B(A): pass
...
>>A_backup = A
A_backup is A
True
>>class A(object): pass
...
>>A_backup is A
False
>>B.__base__ is A
False
>>B.__base__ is A_backup
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.
>>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
>>>
>>>
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,
Dec 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: rmm | last post by:
If I replace the open builtin eg import main __main__.__builtins__.open=None Is there any way, from here on, to access the original open function?? Extending this slightly, lets say I put a...
1
by: Søren Schimkat | last post by:
Hi everyone I´m having some problems with replacing one form with another. The first problem is the the replace function only works once - and the second problem is that the thml code dosn´t get...
6
by: Peter Foti | last post by:
I have seen lots of examples where some HTML text is replaced with a background image using CSS. For example, replacing the text of an <h1> with a graphical logo, like so: CSS: h1 span {...
2
by: thehuby | last post by:
Isn't inserting good data and getting it out of a db a pain in the a$$? I am going to be using the Markdown text to HTML parser (http://daringfireball.net/projects/markdown/dingus) for creating...
2
by: Wilko Deppe | last post by:
Hi! I have two questions about replacing the compiled language Resource Files (dll) while the webserver is running. First of all, is possible at all to replace the dll or is it "blocked" by...
1
by: seanmayhew | last post by:
Im attempting to get the vaules for the second drop down on index changed of the first. This works fine. However rather than just replacing the values of the second drop down with the new values...
2
by: Alain | last post by:
Hi, I am working on a project where I need to convert international characters with acii values. Like André -> andre and Björn -> bjorn. How can I do this without replacing every single...
9
by: Prakash Singh Bhakuni | last post by:
am replacing the default "Browse..." button for input type=file. This works fine except that the form will only submit after the SUBMIT button is clicked twice. Any ideas on why this is happening...
7
by: Leo Smith | last post by:
Hello, I am trying to replace a table that is setting inside a div tag. The reason is that I want to replace a table through AJAX instead of using postbacks or Iframes. The table is built on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.