Skip to content

Latest commit

 

History

History
65 lines (49 loc) · 2.21 KB

File metadata and controls

65 lines (49 loc) · 2.21 KB

30 Days Of Solidity: Revert Statement

Twitter Follow

Author: Vedant Chainani
June, 2022

<< Day 17 | Day 19 >>

Cover


📔 Day 18

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.

<< Day 17 | Day 19 >>