FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Difference between assert and raise - Python





davidv
They seem to do the same thing to me.

assert 1 > 2, "error"

raise StandardError ("error")

raise Exception ("exception")

Also, what's the difference between an exception and error?

Thanks.
Peterssidan
If the assert fails it throws an exception of type AssertionError. If you turn on optimizations no asserts will throw.

StandardError is derived from Exception, so it is an exception.
Fire Boar
Asserts are more for debugging: they're conditions that must hold or else there is a bug in the code. For example, suppose you have made a list class. Then it would be okay to assert that the size of the list is a non-negative integer.

Raise is used for exceptions: these are more for problems that may be caught and dealt with appropriately. Taking the list example again, suppose you initialize the list to have a maximum of 4 elements, then add 5 elements: the list should at that point throw an exception.

As a rule of thumb: assertions are for internal problems, exceptions are for external problems.
Related topics
D difference between Anime and Tokusatsu... anyone know??
Difference between a Jr. Admin & MOD
Difference between watch & wife.
Is there a difference between "I know" and "I
Difference between Contests and Marketplace.
Whats the difference between Core Duo and Hyperthreading?
what is the difference between primary partition?
Diff: Windows 2000 Vs Windows server 2003
What is the Difference Between Love and Affection
What is the difference between these?
difference between windows xp service pack and sp 2 rc 1
difference between Pentium D, Pentium 4 and AMD Athlon
Difference between <div> and <p>...?
Actionscript2.0 - Difference between var :Object and new Obj
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.