I love assert

Marko Rauhamaa marko at pacujo.net
Fri Nov 14 20:30:13 EST 2014


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.

Assertion errors are in the same class with CPython bugs, external
signals (say, SIGKILL), security breaches, hardware failures and the
like.

For expected failure modes, other exceptions are a suitable facility.


Marko



More information about the Python-list mailing list