473,396 Members | 2,158 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.

A class with eventhandlers ?

Is it possible to code a class that raise exception
automatically when an error occurs?

We usually use try-except pair at where we expect an
error might occur. What am thinking is a class that
has built in error handling so we can do this:

c=MyClass()
c.onError = some_hook

or

c.onError('IndexError', IndexErrorHook)

or, more specific, a function/method-specific error
handling feature:

c.load.onError( IOErrorHook)
c.load( filename )

Is there such a mechnism around? If not, is it possible
to make such a thing ?

--
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
Runsun Pan, PhD
py********@gmail.com
Nat'l Center for Macromolecular Imaging
http://ncmi.bcm.tmc.edu/ncmi/
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
Jan 30 '06 #1
2 991
Runsun Pan <py********@gmail.com> wrote:
...
or, more specific, a function/method-specific error
handling feature:

c.load.onError( IOErrorHook)
c.load( filename )

Is there such a mechnism around? If not, is it possible
to make such a thing ?


Never heard of one, but you could surely make a custom metaclass
satisfying your specs (==wrapping each method into an instance of a type
providing such an onError method, as well as a __call__ delegating to
the real method within a suitable try/except). Sounds like a lot of
work to me, though;-).
Alex
Jan 30 '06 #2
It's definitely possible, here's a small example. There are probably
better ways to do it, but I'll let you figure that out ;)

class ErrorHandler:
def __init__(self,method):
self.method = method
self.errorHook = None

def onError(self,hook):
self.errorHook = hook

def __call__(self, *args, **kwargs):
if self.errorHook:
try:
self.method(*args,**kwargs)
except Exception, e:
self.errorHook(e)
else:
self.method(*args,**kwargs)
class MyClass:
def __init__(self):
self.load = ErrorHandler(self.load)

def load(self,filename):
return self.x

def IOErrorHook(e):
print 'Caught error:',e

c = MyClass()
c.load.onError(IOErrorHook)
c.load('filename')
Jan 30 '06 #3

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

Similar topics

2
by: Rudi | last post by:
Is there anyone who can point me to a good tutorial on how to create a multithreaded class library? Class A is a controller that starts up a number of threads of code in Class B. Class B raises...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
1
by: panik | last post by:
Hi, I'm busy building a small windows application, and as I'm fairly new to OOP programming (have worked about 2 years with C# now), I would like some advice. I have two windows forms that...
1
by: Bingo | last post by:
Quick question, I'm new to C# and ASP.NET. Why do the OnInit, OnPreRender and other eventhandlers in the Page class only have (Eventargs) as the only argument? Isn't the signature of an...
3
by: Robert | last post by:
I need some assistance doing some "right way to do it" coding. The following are EventHandlers associated with Delegates in a child form that call a procedure in the MDI form that resets a timer....
1
by: Timo | last post by:
I haven't tried coding eventhandlers in Global.asax yet -- any "gotchas" I should be aware of? Do programming errors there require bouncing IIS? Will handlers in Global.asax be able to access...
3
by: Armin | last post by:
Hello I have a UserControl with a Click Event. Is it possible to find out the List of all Delegates/Eventhandlers using the Event. I read something about a "getinvocationlist" Methode for...
1
by: Kasper Birch Olsen | last post by:
Hi NG Im adding a bunch of linkbuttons to a page, in a for loop, but I cant get the eventhandlers to work. A simplyfied version of the code looks like this: for (int i = 0; i<10; i++) {...
7
by: kenny.deneef | last post by:
Hey all We are working on a project and have alot of forms with textboxes on. Now we want to put some input validation keypress events on those textboxes. We got a parent class where we putted...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...
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.