【Solidity】Error错误处理

  • Error和Event一样,先定义再使用
  • Error必须和Revert配合使用
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;


contract test {
    error Unauthorized(string errorMsg,address _address);
    address public immutable owner;
    uint public balance = 0;

    constructor(){
        owner = msg.sender;
    }

    function changeBalance(uint updateBalanceNumber) public {
        if(owner!=msg.sender){
            revert Unauthorized("not owner",msg.sender);
        }

        balance = updateBalanceNumber;
    }

}

一单不是owner的话就会报错,以下是报错信息

Loading

Facebook评论