473,544 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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__

zeroDivisionE rror = exceptions.Zero DivisionError

class Foo(zeroDivisio nError):
.... bar = 'bar'
....
>>>
exceptions.Ze roDivisionError = Foo
ZeroDivisionE rror = Foo
__builtin__.Z eroDivisionErro r = Foo

try:
.... raise ZeroDivisionErr or
.... except ZeroDivisionErr or, e:
.... print e.bar
....
bar
>>>
try:
.... 1/0
.... except ZeroDivisionErr or, e:
.... print e.bar
....
Traceback (most recent call last):
File "<stdin>", line 2, in ?
ZeroDivisionErr or: integer division or modulo by zero
>>>
Notice that I get my customized exception type when I explicitly raise
ZeroDivisionErr or 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 1409
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__
>>>
>>zeroDivisionE rror = exceptions.Zero DivisionError
I don't know why you're fiddling that much with the names,
`ZeroDivisionEr ror` 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(zeroDivisio nError):
... bar = 'bar'
...
>>>
>>exceptions.Ze roDivisionError = Foo
>>ZeroDivisionE rror = Foo
>>__builtin__.Z eroDivisionErro r = Foo
>>>
>>try:
... raise ZeroDivisionErr or
... except ZeroDivisionErr or, e:
... print e.bar
...
bar
>>>
>>try:
... 1/0
... except ZeroDivisionErr or, e:
... print e.bar
...
Traceback (most recent call last):
File "<stdin>", line 2, in ?
ZeroDivisionErr or: integer division or modulo by zero
>>>
>>>
The object that ZeroDivisionErr or points to in your code is *not* the
raised exception.
Notice that I get my customized exception type when I explicitly raise
ZeroDivisionErr or 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
1909
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 reference to the original open function inside a class called Isolate and protect this reference using __getattribute__ and __setattr__. Is the...
1
1482
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 interpreted.. but is treated as plain text. You can se a short example here: http://www.schimkat.dk/forms.htm Some hints on that one would be...
6
3601
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 { display: none; } h1 { height: 100px; background: url("mycompany.gif") no-repeat 35px 40px; } HTML: <h1><span>My Company</span></h1>
2
2037
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 HTML from user input (for a bespoke CMS) so that users can put in their own headings, lists and links etc. This is great and gets round all the...
2
1190
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 the IIS or another Service? Secondly do I have to reregister the dll on the server? Thanks a lot for your answers
1
1167
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 its just appending them. On each post back. ie myddlOne.item = fruit; myddlOne.item = vegetable; select fruit returns
2
4195
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 character? lastname = lastname.Replace("â", "a"); lastname = lastname.Replace("é", "e"); lastname = lastname.Replace("ö", "o");
9
24735
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 or a workaround? I'm testing on IE Thanks! <html> <head></head> <body> <form name="test_form" method="post" action="test.jsp">
7
1496
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 the server then sent back to the webpage as a string. I am trying to avoid parsing the string and recreating every control within the object. I had...
0
7598
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7757
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5895
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5288
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3400
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3398
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1833
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
651
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.