DEV Community

Cover image for Day 18 - Revert Statement
Vedant Chainani
Vedant Chainani

Posted on • Updated on

Day 18 - Revert Statement

GitHub logo Envoy-VC / 30-Days-of-Solidity

30 Days of Solidity step-by-step guide to learn Smart Contract Development.

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)