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

Antoine solipsis at pitrou.net
Thu Oct 12 11:01:14 CEST 2006


> 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.





More information about the Python-3000 mailing list