Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java

Idiom #123 Assert condition

Verify that predicate isConsistent returns true, otherwise report assertion violation.
Explain if the assertion is executed even in production environment or not.

assert isConsistent() : "State consistency violated";

This throws an AssertionError when then condition is false.
By default, assertions are disabled at runtime.
Use java switch -enableassertions or -ea.
#include <assert.h>
assert(isConsistent());

If NDEBUG is defined, the assert macro becomes void. Therefore, such expressions must not have side effects.

New implementation...
< >
programming-idioms.org