assert
statement
Syntax
AssertStatement :
assert
Expression (,
Expression)?
The assert
statement is used express invariants in the code. It consists of a boolean expression optionally followed by a comma followed by a string expression.
If the boolean expression evaluates to false
, the code reverts with a panic code of 0x01
. In the case that the first expression evaluates to false
and a second string expression is given, the code reverts with the given string as the error code.
Warning:
The current implementation of assert
is under active discussion and likely to change.
An example of a assert
statement without the optional message:
contract Foo {
fn bar(val: u256) {
assert val > 5
}
}
An example of a assert
statement with an error message:
contract Foo {
fn bar(val: u256) {
assert val > 5, "Must be greater than five"
}
}