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



More information about the Python-list mailing list