// SPDX-License-Identifier: MIT pragma solidity ^0.8.13;
contract Error { function testRequire(uint _i) public pure { require(_i > 10, "Input must be greater than 10"); } function testAssert() public view { assert(_i > 10); } function testRevert(uint _i) public pure { if (_i <= 10) { revert("Input must be greater than 10"); } } }
自定义错误
自定义错误信息可以放在合约外部或者内部;
外部可以被同文件其他合约引用;
内部可以被本合约及继承合约引用;
这是目前最优的错误处理做法, 好处是更节省 Gas,同时可以传递合约中的变量.
// SPDX-License-Identifier: MIT pragma solidity ^0.8.13; error OutFuncError(address, uint256);