Version: 0.13.0

Exceptions

In some cases it's necessary to interrupt the flow of execution and fail the contract. This is where the failwith function comes in.

The failwith function#

The failwith function raises an error that cannot be caught, and terminates the contract.

type parameter is
Zero of nat
| Pos of nat
type storage is unit
type return is list (operation) * storage
function main (const p : parameter; const s : storage) : return is
block {
case p of
Zero (n) -> if n > 0n then failwith ("fail") else skip
| Pos (n) -> if n > 0n then skip else failwith ("fail")
end
}
with ((nil : list (operation)), s)

Assertions#

Assertions can be used to ensure a certain condition is met when running a contract. assert is used to check if a boolean condition is true and assert_some is used to check if an option value is not None. When a condition is not met, the contract will stop executing and display an error.

function main (const p : bool; const s : storage) : return is
block {
assert (p)
}
with ((nil : list (operation)), s)
function some (const o : option (unit)) is assert_some (o)