[Tutor] finally without try or except
Tino Dai
oberoc at gmail.com
Mon Jul 30 19:23:45 CEST 2012
On Mon, Jul 30, 2012 at 1:16 PM, Marc Tompkins <marc.tompkins at gmail.com>wrote:
> On Mon, Jul 30, 2012 at 9:56 AM, Tino Dai <oberoc at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120730/fb7845d4/attachment-0001.html>
More information about the Tutor
mailing list