-
Notifications
You must be signed in to change notification settings - Fork 0
/
Error.sol
41 lines (31 loc) · 947 Bytes
/
Error.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
//three ways to handle error in solidity
//require:
//revert:
//assert:
// gas refund and state updates are reverted after any error
//custom errors save gas fee
contract Error{
function testRequireError(uint _i) public pure{
require(_i <= 10, "i less than 10");
}
function testRevertError(uint _i) public pure{
if(_i > 10){
revert("i greater than 10");
//revert is mainly use in the context of a loop
}
}
uint public num = 123;
function testAssertError() public view{
assert(num == 123);
//assert is use to check for condition that should always be true
}
error MyError(address caller, uint i);
function testCustError(uint _i) public view{
//custom error can only be use with revert
if(_i > 10){
revert MyError(msg.sender, _i);
}
}
}