Assertion vs. try-except

Erik Max Francis max at alcyone.com
Fri Oct 20 11:42:20 EDT 2000


Thomas Weholt wrote:

> Just wondering what the benefits of assert is, compared to doing a
> try-except for instance. The documentation says :
> "Assert statements are a convenient way to insert debugging assertions
> into a program"
> 
> All nice and handy, but is this debugging specific ( should they be
> removed when testing / debugging is over, if that will ever happen ??
> ) ? Why not use a try-except instead and print / handle errors?

assert raises an AssertionError.  You can catch it later on if you like.

> It's short and sweet, but are there any drawbacks in using assert this
> way?

The idea being asserts is to watchdog situations that should never
happen.  These will often be more serious than mere bad input or other
things you'd want to raise an exception for (and recover from).  Often
they're used for consistency checks, and when things become
inconsistent, you want to stop right there and alert the programmer,
because it's already probably too late to recover.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ What a crime to waste [youth] on children.
\__/ George Bernard Shaw
    Maths reference / http://www.alcyone.com/max/reference/maths/
 A mathematics reference.



More information about the Python-list mailing list