Skip to content

assert

Assert that a condition is true; throw an error if not.

assert(expression)
assert(expression, msg)
assert(expression, msgId, msg, ...)

If expression evaluates to false, throws an error with the supplied message (or a default message). Use to enforce preconditions and invariants. Errors thrown by assert propagate normally and can be caught with try/catch.

assert(x > 0, 'x must be positive')
assert(size(A) == size(B), 'matrices must match')
  • error — Throw an error.
  • warning — Display a warning message.