An assertion is a statement, which aborts a program when it evaluates to false. Assert is typically used for debugging and situations which should never happen.
An assertion is a statement, which aborts a program when it evaluates to false. Assert is typically used for debugging and situations which should never happen.
It is normally bad practice to use asserts in deployed software, because it typically provides information that is useful only to programmers; exceptions are preferred in this case. Also, assertions are not for use in validating input or in other situations where exceptions are preferred.
However, asserts can be used frequently when testing, to make sure that design requirements are met (for example, when designing-by-contract), and in debugging to make sure that code which is incorrect fails as quickly as possible. Assertions often give line numbers and file names, which makes tracking down where code failed easier than with other methods like core dumps.