envoy1084
/
30-Days-of-Solidity
30 Days of Solidity step-by-step guide to learn Smart Contract Development.
WARNING: This repository is currently undergoing updates and revisions to incorporate the latest information and advancements in Solidity programming. Please be advised that the content may not be up-to-date or accurate during this time. We expect the updates to be completed within the next 30 days, and appreciate your patience during this process. Thank you for your understanding.
Contents
- Day 1 - Licenses and Pragma
- Day 2 - Comments
- Day 3 - Initializing Basic Contract
- Day 4 - Variables and Scopes
- Day 5 - Operators
- Day 6 - Types
- Day 7 - Functions
- Day 8 - Loops
- Day 9 - Decision Making
- Day 10 - Arrays
- Day 11 - Array Operations
- Day 12 - Enums
- Day 13 - Structs
- Day 14 - Mappings
- Day 15 - Units
- Day 16 - Require Statement
- Day 17 - Assert Statement
- Day 18 - Revert Statement
- Day 19 - Function Modifiers
- Day 20…
This is Day 18 of 30 in Solidity Series
Today I Learned About Revert Statement in Solidity.
Revert Statement
This statement is similar to the require statement. It does not evaluate any condition and does not depends on any state or statement. It is used to generate exceptions, display errors, and revert the function call. This statement contains a string message which indicates the issue related to the information of the exception. Calling a revert statement implies an exception is thrown, the unused gas is returned and the state reverts to its original state. Revert is used to handle the same exception types as require handles, but with little bit more complex logic.
Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
contract RevertStatement {
function checkOverflow(uint256 _num1, uint256 _num2)
public
view
returns (string memory, uint256)
{
uint256 sum = _num1 + _num2;
if (sum < 0 || sum > 255) {
revert(" Overflow Exist");
} else {
return ("No Overflow", sum);
}
}
}
Output:
when we pass 96 and 178 to the function checkOverflow, it will throw an exception with the message "Overflow Exist".
call to RevertStatement.checkOverflow errored: VM error: revert.
revert
The transaction has been reverted to the initial state.
Reason provided by the contract: " Overflow Exist".
Debug the transaction to get more information.
Top comments (0)