-
Notifications
You must be signed in to change notification settings - Fork 4
/
9_Error_Handling.sol
43 lines (36 loc) · 1.39 KB
/
9_Error_Handling.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
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.10;
contract ErrorHandling {
string public str = "Bhupendra";
function requireFunc1(uint _i) public returns (string memory) {
str = "requireFunc1 Called!";
require(_i==0);
return str;
}
function requireFunc2(uint _i) public returns (string memory) {
str = "requireFunc2 Called!";
require(_i==0, "condition not met");
return str;
}
function revertFunc(uint _i) public returns (string memory) {
str = "revertFunc Called!";
if (_i != 0) {
revert("Revert condtion met.");
}
return str;
}
function assertFunc(uint _i) public returns (string memory) {
str = "assertFunc Called!";
assert(_i == 0);
return str;
}
}
/*
- Error Handling:
An error will undo all changes made to the state during a transaction.
- You can throw an error by calling require, revert or assert:
- "require" is used to validate inputs and conditions before execution. Gives back the remaining gas fees.
- "revert" is similar to require. See the code below for details. Gives back the remaining gas fees.
- "assert" is used to check for code that should never be false. Failing assertion probably means that there is a bug.
Mostly used for internal contract errors. Cost is high because it consumes whole gas fees.
*/