On Mon, Jul 30, 2012 at 1:16 PM, Marc Tompkins <marc.tompk...@gmail.com>wrote:

> On Mon, Jul 30, 2012 at 9:56 AM, Tino Dai <obe...@gmail.com> wrote:
>
>> Hi!
>>
>>      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?
>> I was thinking about maybe a free standing finally code block or a
>> decorator.
>>
>
> How about calling the entire program in a module and putting the call to
> it inside a try/except block?  Then you can have your cleanup code in a
> finally block at the end of all that.
>
> Exceptions are meant to be caught.  I think what you're asking is "is
> there any way to make Python ignore any bad stuff and keep running my
> program?"  The answer is yes: catch your exceptions.
>

Actually, what I'm doing is keeping a pending item log in memory as an
array and then saving it to the DB at the end of the program, but what
happens if the user hits ctrl-c, then the pending items array is lost.
That's the use case that I'm looking for a solution to.

-Tino
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to