REPOST: Re: try-except-finally question

Aahz Maruch aahz at panix.com
Wed Dec 26 19:09:59 EST 2001


In article <xjvr8phwmv3.fsf at vestavind.ifi.uio.no>,
Igor V. Rafienko <igorr at ifi.uio.no> wrote:
>
>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>
            except:
                 <log error>
                 raise
>	    # 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.

Note the addition of "except:" above.  You need the "raise" to make sure
the error gets re-raised (you *want* to abort if the user hits ^C).
-- 
                      --- Aahz  <*>  (Copyright 2001 by aahz at pobox.com)

Hugs and backrubs -- I break Rule 6                 http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het Pythonista   

Tenth Virtual Anniversary: 5 days and counting

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-nwblwi.newsops.execpc.com!newsfeeds.sol.net!newspump.sol.net!newsfeed.direct.ca!look.ca!newsfeed.dacom.co.kr!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: aahz at panix.com (Aahz Maruch)
Newsgroups: comp.lang.python
Subject: cmsg cancel <a0dosn$goj$1 at panix3.panix.com>
Control: cancel <a0dosn$goj$1 at panix3.panix.com>
Date: Mon, 31 Dec 2001 05:08:54 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.a0dosn$goj$1 at panix3.panix.com>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009776004 27193 211.57.49.2 (31 Dec 2001 05:20:04 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 05:20:04 +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