assert
assert(cond)
Throw an AssertionError
if cond
is false
. Also available as the macro @assert expr
.
Examples
julia> assert(2 + 2 == 4)
This example ensures that the condition 2 + 2 == 4
is true
. If the condition is false
, an AssertionError
will be thrown.
julia> x = 5
julia> y = 10
julia> assert(x < y, "x must be smaller than y")
In this case, the assertion checks if x < y
is true
. If it is false
, an AssertionError
with the provided custom error message "x must be smaller than y" will be thrown.
Common mistake example:
julia> assert(2 + 2 == 5, "Math is broken")
ERROR: AssertionError: Math is broken
Here, the assertion fails because the condition 2 + 2 == 5
is false
. The provided error message "Math is broken" is displayed along with the AssertionError
. Make sure to double-check the condition to avoid such mistakes.
See Also
assert, backtrace, code_llvm, code_lowered, code_native, code_typed, code_warntype, :@which, compilecache, current_module, eval, finalize, finalizer, fullname, function_module, function_name, include_dependency, InterruptException, invoke, isconst, isdefined, isgeneric, methodswith, method_exists, module_name, module_parent, require, subtypes, unsafe_load, workspace, __precompile__,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.