Cookbook
Miscellaneous

Miscellaneous

Various niche examples which don't yet have a dedicated page, but are useful and interesting nonetheless.

How to throw errors

The throw() function in a contract is useful when we don't know how often to perform a specific action.

It allows intentional exception or error handling, which leads to the termination of the current transaction and reverts any state changes made during that transaction.

let number: Int = 198;
 
// the error will be triggered anyway
throw(36);
 
// the error will be triggered only if the number is greater than 50
nativeThrowIf(35, number > 50);
 
// the error will be triggered only if the number is NOT EQUAL to 198
nativeThrowUnless(39, number == 198);
🤔

Didn't find your favorite example of working with something niche? Have cool implementations in mind? Contributions are welcome! (opens in a new tab)