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 anywaytry { throw(36); } catch (exitCode) {}
// the error will be triggered only if the number is greater than 50try { nativeThrowIf(35, number > 50); } catch (exitCode) {}
// the error will be triggered only if the number is NOT EQUAL to 198try { nativeThrowUnless(39, number == 198); } catch (exitCode) {}