[python-win32] Recovering from AODB Exceptions, possible?
Rex Corrovan
rex555 at hotmail.com
Tue Feb 13 02:46:40 CET 2007
Actually, I do have a lot more exception handling going on. I just tried to
keep the code simple to demonstrate the problem. But yeah, if I except in a
way I am not expecting (ie: anything but a record exists) I handle it
differently.
Thanks for the tips though, and most definatly for making sure I am using
good practices :)
Rex
From: "Mark Hammond" <mhammond at skippinet.com.au>
To: "'Rex Corrovan'" <rex555 at hotmail.com>,<python-win32 at python.org>
Subject: RE: [python-win32] Recovering from AODB Exceptions, possible?
Date: Sat, 10 Feb 2007 18:04:19 +1100
> So I have a function that ries to add a record:
>
> def EntryAdd(self, dataDict):
> try:
> self._DBConn.MoveFirst()
> self._DBConn.AddNew()
> for key, value in dataDict.items():
> self._DBConn.Fields.Item(key).Value = value
> self._DBConn.Update()
> except pywintypes.com_error, e:
> print e
> print 'error text here'
> return True
>
> So if the error already exists I get an exception, no
> problem, I handle and
> log it.
In general, you should try and be specific about handling exceptions.
Assuming you want to only handle "record exists" type exceptions, you should
probably write something closer to:
def EntryAdd(self, dataDict):
self._DBConn.MoveFirst()
self._DBConn.AddNew()
for key, value in dataDict.items():
self._DBConn.Fields.Item(key).Value = value
try:
self._DBConn.Update()
except pywintypes.com_error, e:
hr, msg, exc = e
if hr != SOME_CONSTANT_MEANING_RECORD_EXISTS:
raise
print e
print 'record exists'
return True
I'm not sure what SOME_CONSTANT_MEANING_RECORD_EXISTS is, and it may even be
necessary to unpacl 'exc' to get a reliable error code. All other errors
will still result in an uncaught exception - so any other errors you have
are not silently handled as if they were the common "record exists". You
probably still want an outer exception handler that handles all errors in a
sane way
Mark
_________________________________________________________________
Invite your Hotmail contacts to join your friends list with Windows Live
Spaces
http://clk.atdmt.com/MSN/go/msnnkwsp0070000001msn/direct/01/?href=http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us
More information about the Python-win32
mailing list