//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.3;
contract ErrorControl {
// require
function requireExample(uint256 index) external pure returns (uint256) {
// index小于等于10的时候会报错
require(index > 10, "index must gt 10");
return index;
}
// revert
function revertExample(uint256 index) external pure returns (uint256) {
if (index > 100) {
// index 大于等于100的时候会报错
revert("index must lt 100");
}
return index;
}
// assert
function assertExample(uint256 index) external pure returns (uint256) {
// index不等于30的时候会报错
assert(index == 30);
return index;
}
// 自定义错误
error MyError(address caller, uint256 index, string message);
function customError(uint256 index) public view returns (uint256) {
if (index > 10) {
revert MyError(msg.sender, index,"index can not gte 10");
}
return index;
}
}