Which exception to use?

A. Lloyd Flanagan alloydflanagan at attbi.com
Thu Jan 30 20:06:46 CET 2003

Erik Max Francis <max at alcyone.com> wrote in message news:<3E371F9A.938B2D61 at alcyone.com>...
> Ben Caradoc-Davies wrote:
> > My favourite in these circumstances is AssertionError. You can even
> > rewrite
> If the error discussed here is genuine, assert is not the right way to
> handle it.  Asserts can be disabled with the -O option; if someone is
> running your script "optimized," execution will continue past the assert
> statements where it should have stopped.

An assertion, in formal programming theory, is a statement that some
condition is always going to be true.  Generally you would never use
assertions in a situation where you expect them to fail; that's what
exceptions are for.  So assertions should never be used to control the
flow of program logic -- that's not what 'assert' means.

The point is that within a procedure you can't guarantee that the
parameters are correct; that's up to the caller.

All of which boils down to "I agree with Erik".

More information about the Python-list mailing list