REPOST: 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

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-nwblwi.newsops.execpc.com!newsfeeds.sol.net!priapus.visi.com!news-out.visi.com!hermes.visi.com!newsfeed.direct.ca!look.ca!newsfeed.dacom.co.kr!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: igorr at ifi.uio.no (Igor V. Rafienko)
Newsgroups: comp.lang.python
Subject: cmsg cancel <xjvr8phwmv3.fsf at vestavind.ifi.uio.no>
Control: cancel <xjvr8phwmv3.fsf at vestavind.ifi.uio.no>
Date: Mon, 31 Dec 2001 02:44:44 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.xjvr8phwmv3.fsf at vestavind.ifi.uio.no>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009776009 27193 211.57.49.2 (31 Dec 2001 05:20:09 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 05:20:09 +0000 (UTC)
X-No-Archive: yes
X-Unac4ncel: yes
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)

This message was cancelled from within Mozilla.



More information about the Python-list mailing list