forked from hyperledger-solang/solang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry_catch.sol
48 lines (43 loc) · 1.17 KB
/
try_catch.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
contract TryCatchCaller {
constructor() payable {}
function test(uint128 div) public payable returns (uint128) {
TryCatchCallee contract_instance = new TryCatchCallee();
try contract_instance.test(div) returns (uint128) {
return 4;
} catch Error(string reason) {
assert(reason == "foo");
return 1;
} catch Panic(uint reason) {
assert(reason == 0x12);
return 0;
} catch (bytes raw) {
if (raw.length == 0) {
return 3;
}
if (raw == hex"bfb4ebcf") {
return 2;
}
}
assert(false);
}
}
contract TryCatchCallee {
error Foo();
// div = 0: Reverts with Panic error
// div = 1: Reverts with Error error
// div = 2: Reverts with Foo error
// div = 3: Reverts with empty error
// div > 3: Doesn't revert
function test(uint128 div) public pure returns (uint128) {
if (div == 1) {
revert("foo");
}
if (div == 2) {
revert Foo();
}
if (div == 3) {
revert();
}
return 123 / div;
}
}