The asserted condition did not evalutate to true. Optional argument msg is a descriptive error string.


  1. Throw a basic assertion error:

    julia> assert(false, "This is an assertion error")
    ERROR: AssertionError: This is an assertion error

    This example throws an AssertionError with the provided error message when the condition false is evaluated.

  2. Assert inequality using an error message:

    julia> x = 5
    julia> assert(x != 5, "x should not be equal to 5")
    ERROR: AssertionError: x should not be equal to 5

    In this example, an AssertionError is thrown with the given error message if the condition x != 5 is not satisfied.

  3. Assert a condition without an error message:

    julia> assert(2 + 2 == 5)
    ERROR: AssertionError

    This example throws a basic AssertionError when the condition 2 + 2 == 5 is not true.

Common mistake example:

julia> assert(1 == 2, "This should fail")
ERROR: AssertionError: This should fail

In this example, the condition 1 == 2 is expected to be false, but it evaluates to true. Double-check that the condition used in the assert statement is correct to avoid such mistakes.

See Also

ArgumentError, AssertionError, BoundsError, DivideError, DomainError, EOFError, error, ErrorException, InexactError, InitError, KeyError, LoadError, MethodError, OutOfMemoryError, OverflowError, ParseError, ReadOnlyMemoryError, showerror, StackOverflowError, SystemError, TypeError, UndefRefError, UndefVarError,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: