[Python-3000] try ... then ... then ... except / finally

Georg Brandl g.brandl at gmx.net
Thu Oct 12 12:39:20 CEST 2006


Antoine wrote:
>> Can you explain the difference to:
>>
>> try:
>>      self.db_transaction.commit()
>>      self.tmpoutfile.close()
>>      self.destroy_shared_memory_handle()
>> except Exception, e:
>>      logging.error("an exception occurred in cleanup: %s", e)
> 
> In your construct, if self.db_transaction.commit() raises an exception,
> self.tmpoutfile won't get closed and self.destroy_shared_memory_handle()
> won't be called.
> 
> In my proposal, if an exception is raised in a "try" block, the execution
> skips to the next "then" block, and so on.
> At the end it executes the "except" block if at least one "try" or "then"
> block raised an exception.

Ah. On Error Resume Next, revisited ;)

Like Fredrik, I really think that this is a good application for context
managers.

Georg



More information about the Python-3000 mailing list