try-except-finally question

Igor V. Rafienko igorr at ifi.uio.no
Wed Dec 26 19:03:12 EST 2001


Hi,

I'm writing a small program that updates a database. An update session
depends on a number of external factors, and it is very important to
log (to a file) if anything goes wrong.

The skeleton code looks something like this:

def updateDB():
 
    # some initializing

    try:
	<for each group to update>
	    try:
	        <begin transaction>
                <check external factors>
    	        <for each item in group>
                    <update item in db>
	        <commit transaction>
            except ExternalFactorError:
	        <log error>
	    except DBError:
	        <log error>
	    # yrt
        # od
    finally:
	<close DB connection>
	<flush log>
    # yrt
# end updateDB

The outermost try-finally is needed to free the initialized resources.

I have 2 questions:

* In the innermost try-except there is a possibility for other
  exceptions than ExternalFactorError and DBError. How can I catch
  _all_ other exceptions than these two[*]? I'm not so much interested
  in error itself, but rather whether it occured. I.e. I'm looking for
  C++'s "catch (...)" construct in Python.

* Is there a more elegant way of writing this function? 

TIA,	





ivr
[*] is there a type that _all_ python object inherit from? (I know
about exceptions.Exception, but I do not want to rely on whether third
party modules actually inherit from it)
-- 
Documentation is like sex: when it is good, it is very, very good; and
when it is bad, it is better than nothing.
						-- Dick Brandon



More information about the Python-list mailing list