Sui Move Language - Assertions & Error Handling

Jay NalamJay Nalam
2 min read

Hello everyone..

Welcome to another day of exploring Web3 Engineering. In this series, we are looking into the move language for developing smart contracts on Sui blockchain. So, without any further ado, let us get started.

Just like any other language, Move also has support for assertion conditions. We can't call them error handling because, the contracts are only allowed to throw errors, if the conditions are not met. Move lang provides two builtin functions assert! and abort!.

Assertions

assert is a builtin, macro operation provided by the Move compiler. It takes two arguments, a condition of type bool and a code of type u64.

The syntax of the assert is as follows

assert!(condition, error_code);

// Example
assert!(1 == 2, 0);

Errors

Sui move language doesn't have an in built error type to define errors but throws errors as u64 numbers. So, to define various errors, the errors are usually defined as constants and are used.

const ENotEqual: u64 = 0;

assert!(1 == 2, ENotEqual);

The naming convention for error codes must be in Camel case prefixed with E.

Abort

Move also provides abort statement for throwing errors. The syntax of the abort is as follows:

abort ErrorCode;

// Example
const EUnAuthorizedUser :u64 = 0;
const user_has_access = true;

if(!user_has_access)
    abort EUnAuthorizedUser;

In our upcoming article, let us write our first smart contract on Sui blockchain.

1
Subscribe to my newsletter

Read articles from Jay Nalam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jay Nalam
Jay Nalam

Hi, I'm Jay Nalam, a seasoned Web3 Engineer committed to advancing decentralized technologies. Specializing in EVM-based blockchains, smart contracts, and web3 protocols, I've developed NFTs, DeFi protocols, and more, pushing boundaries in the crypto realm.