目录
Error:error 是 solidity 0.8.4 版本新加的内容,方便且高效(省gas)地向用户解释操作失败的原因。人们可以在contract之外定义异常。
一、合约示例1
普通用法:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract Error {
function testRequire(uint _i) public pure {
// Require should be used to validate conditions such as:
// - inputs
// - conditions before execution
// - return values from calls to other functions
require(_i > 10, "Input must be greater than 10");
}
function testRevert(uint _i) public pure {
// Revert is useful when the condition to check is complex.
// This code does the exact same thing as the e
本文详细介绍了Solidity 0.8.4版本引入的Error特性,如何方便且高效地通过Error向用户反馈操作失败的原因。内容涵盖自定义Error的创建、与revert语句的结合使用,以及在安全购物合约中的应用。通过合约示例,阐述了Error如何提升智能合约的可读性和成本效益。
订阅专栏 解锁全文
627

被折叠的 条评论
为什么被折叠?



