跳转到内容

杂项

各种尚未有专门页面但仍然有用且有趣的示例。

如何抛出错误

当我们不知道要多久执行一次特定操作时,合约中的 throw() 函数就非常有用。

它允许有意的异常或错误处理,从而导致当前事务的终止,并恢复该事务中的任何状态更改。

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