[Python-Dev] Bare except clauses in PEP 348

Shane Hathaway shane at hathawaymix.org
Wed Aug 24 18:42:21 CEST 2005


Niko Matsakis wrote:
>>
>> txn = new_transaction()
>> try:
>>      txn.begin()
>>      rtn = do_work()
>> finally:
>>      if exception_occurred():
>>          txn.abort()
>>      else:
>>          txn.commit()
>>          return rtn
>>
> 
> Couldn't you just do:
> 
> txn = new_transaction ()
> try:
>     complete = 0
>     txn.begin ()
>     rtn = do_work ()
>     complete = 1
> finally:
>     if not complete: txn.abort ()
>     else: txn.commit ()
> 
> and then not need new builtins or anything fancy?

That would work, though it's less readable.  If I were looking over code 
like that written by someone else, I'd have verify that the "complete" 
variable is handled correctly in all cases.  (As Martin noted, your code 
already has a bug.)  The nice try/except/else idiom we have today, with 
a bare except and bare raise, is much easier to verify.

Shane


More information about the Python-Dev mailing list