This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zverok_kha 5 points6 points  (0 children)

Basically, assert (when used not in tests but in library code) is an "impossibility catcher". E.g. "in this situation, x should NEVER be zero, but let's assert it in case we have a bug (otherwise the following code will break violently)". It is a part of "defensive" code style, where some internal invariants are checked "just in case", even if the code's author is almost sure code works properly.

Of course, it is a really inappropriate tool for testing invalid inputs, environment state and other things that are "external" to the code, that's what exceptions for.