[Tutor] finally without try or except

Steve Willoughby steve at alchemy.com
Mon Jul 30 20:55:37 CEST 2012


On 30-Jul-12 10:52, Mark Lawrence wrote:
> On 30/07/2012 17:56, Tino Dai wrote:
>>       Is there anyway to execute a block of code at the end of a
>> program in
>> 2.6 regardless of what happened before eg exiting normally or died
>> because
>> of an exception?

> Sorry I'm not completely sure what you're asking for but will this help
> http://docs.python.org/library/atexit.html ?

I don't think that will cover the use cases the OP asked about.  I 
suppose you could do something like

try:
    main()
finally:
    save_everything()

But I think it would be better to re-think your design.  Why must the 
final database commit (or whatever it is) happen at the end and not 
along the way?  Are there critical subsets of the code you could protect 
more locally and save after they're done?

Are you certain that the data you'll save will be accurate if the 
program crashed or was interrupted in the middle of something?

I think the block above will do what you're asking for, but I'm not sure 
what you're asking for is what will help you best.




-- 
Steve Willoughby / steve at alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C


More information about the Tutor mailing list