简体   繁体   English

在Solidity中,使用if()和require()有什么区别?

[英]In Solidity, what is the difference between using if() and require()?

I can run code inside a conditional if statement. 我可以在条件if语句中运行代码。 I can also require a condition before running some code. 在运行一些代码之前,我还可以要求一个条件。 Are they interchangeable or are there reasons I would choose to rely on one and not the other? 它们是可互换的,还是有我选择一个而不是另一个依赖的原因?

No, they are not interchangeable. 不,它们不可互换。

When an if condition is false, the next condition/block is executed and the else block (if any) is run. 如果if条件为false,则执行下一个条件/块,并运行else块(如果有)。 Standard if/else programming logic. 标准的if / else编程逻辑。

require is an error handling statement. require是一个错误处理语句。 When a require condition is false, all execution stops and the transaction is rolled back. require条件为false时,所有执行将停止并且事务将回滚。

See the Solidity documentation for error handling for more info on require and the differences between that and other error handling functions such as assert and revert . 密实度的文档进行错误处理的更多信息require ,并且和其他错误处理功能,如之间的差异assertrevert

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM