I love assert
Chris Angelico
rosuav at gmail.com
Fri Nov 14 20:48:44 EST 2014
On Sat, Nov 15, 2014 at 12:30 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Chris Angelico <rosuav at gmail.com>:
>
>> On Sat, Nov 15, 2014 at 11:12 AM, Marko Rauhamaa <marko at pacujo.net> wrote:
>>> Most importantly, assertion failures are not supposed to be recovered
>>> from (within the program). Assertion failures can result in the loss
>>> of life and limb. They can result in database corruption. They can
>>> result in monetary losses. They can result in smoke coming out of the
>>> monitor.
>>
>> Or, in theory, AssertionError just prevented any of the above from
>> happening.
>
> I'd advice against catching AssertionError and trying to recover from it
> within the program. You could catch it, log it and reraise it, but since
> the failure modes could be completely unexpected (really, by
> definition), I would move fault-tolerance outside the failing process
> and try to restore a coherent reality from there.
I agree - never catch it. But you should be able to prevent database corruption:
conn = establish_database_connection()
try:
do_stuff()
finally:
conn.rollback()
The raising of AssertionError anywhere inside do_stuff() will prevent
craziness from getting to the database, because it aborts the
execution. (You know, the way a royal pardon does.)
ChrisA
More information about the Python-list
mailing list