The revert statement in Solidity is used to throw an exception, return unused gas, and revert to the original state
The revert statement is a function in Solidity that is used to generate exceptions and display an error message. It is similar to the require function, but it does not evaluate any statement and does not depend on any state or statements. When a revert statement is hit, an exception is thrown, along with the return of unused gas, and reverts to its original state. The Solidity assert exceptions use all of the gas provided to the call. Metropolis (the third stage of the Ethereum launch process), require not to use of gas.
Solidity has functions designed for reverting state changes to prevent possible issues. The revert function accepts a string message consisting of information about the issue delivered to the caller. Instances of errors can only be created using revert statements. The error creates data that is then passed to the caller with the revert operation to either return to the off-chain component or catch it in a try/catch statement. Custom errors are defined using revert statements, but they are rather expensive, especially when it comes to deployment cost, and it is difficult to use dynamic information in them.
When a contract invokes a function that reverts, and updates down all adjacent branches will fail with it. An original signed transaction from an externally owned account will fail if it encounters a revert statement.
Similar to the require statement, this one specifies the condition. It does not assess any conditions and does not rely on any state or statement to function properly. It can be used to generate exceptions, display errors, and revert the function call, amongst other things. This statement comprises a string message that describes the problem with the information provided by the exception. It is implied by the usage of the revert statement that an exception is raised, the used gas is returned, and the state is restored to its initial state. Revert is used to handle the same types of exceptions as require does, but with a little bit more advanced logic than require does.
pragma solidity ^0.8.0;
// Creating a contract
contract revertStatement {
// Defining a function to check condition
function checkOverflow(uint num1, uint num2) public view returns(string memory, uint){
uint sum = num1 + num2;
if(sum < 0 || sum > 255){
revert(" Overflow Exist");
}
else{
return ("No Overflow", sum);
}
}
}
For more content, follow me at - https://linktr.ee/shlokkumar2303
Top comments (0)