Assertions are a means to verify that program invariants have not been violated. Put simply: you assert
that impossible things are not happening, and if that assertion ever fails — if the impossible is happening — then your program should halt execution immediately, generally for two reasons:
-
If your program has done something that should (if your program is properly written) be logically impossible, then you will likely only detect the problem after the fact. You don't know what went wrong, you don't know how it went wrong, and you don't know the scope of the damage, and this means both that your program can't reliably correct the error and that your program can no longer trust itself at all. The least dangerous thing to do is stop executing immediately — crash, on purpose, before your program can do anything else that shouldn't be possible.
-
Depending on your choice of programming language and environment, halting execution may give you an opportunity t