-
Notifications
You must be signed in to change notification settings - Fork 2k
/
TryCatch.sol
62 lines (55 loc) · 1.95 KB
/
TryCatch.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.21;
contract OnlyEven{
constructor(uint a){
require(a != 0, "invalid number");
assert(a != 1);
}
function onlyEven(uint256 b) external pure returns(bool success){
// 输入奇数时revert
require(b % 2 == 0, "Ups! Reverting");
success = true;
}
}
contract TryCatch {
// 成功event
event SuccessEvent();
// 失败event
event CatchEvent(string message);
event CatchByte(bytes data);
// 声明OnlyEven合约变量
OnlyEven even;
constructor() {
even = new OnlyEven(2);
}
// 在external call中使用try-catch
// execute(0)会成功并释放`SuccessEvent`
// execute(1)会失败并释放`CatchEvent`
function execute(uint amount) external returns (bool success) {
try even.onlyEven(amount) returns(bool _success){
// call成功的情况下
emit SuccessEvent();
return _success;
} catch Error(string memory reason){
// call不成功的情况下
emit CatchEvent(reason);
}
}
// 在创建新合约中使用try-catch (合约创建被视为external call)
// executeNew(0)会失败并释放`CatchEvent`
// executeNew(1)会失败并释放`CatchByte`
// executeNew(2)会成功并释放`SuccessEvent`
function executeNew(uint a) external returns (bool success) {
try new OnlyEven(a) returns(OnlyEven _even){
// call成功的情况下
emit SuccessEvent();
success = _even.onlyEven(a);
} catch Error(string memory reason) {
// catch revert("reasonString") 和 require(false, "reasonString")
emit CatchEvent(reason);
} catch (bytes memory reason) {
// catch失败的assert assert失败的错误类型是Panic(uint256) 不是Error(string)类型 故会进入该分支
emit CatchByte(reason);
}
}
}