Solidity has many functions for error handling. Errors can occur at compile time or runtime. Solidity is compiled to byte code and there a syntax error check happens at compile-time, while runtime errors are difficult to catch and occurs mainly while executing the contracts. Some of the runtime errors are out-of-gas error, data type overflow error, divide by zero error, array-out-of-index error, etc. Until version 4.10 a single throw statement was there in solidity to handle errors, so to handle errors multiple if…else statements, one has to implement for checking the values and throw errors which consume more gas. After version 4.10 new error handling construct assert, require, revert statements were introduced and the throw was made absolute.
Require Statements
The ‘require’ statements declare prerequisites for running the function i.e. it declares the constraints which should be satisfied before executing the code. It accepts a single argument and returns a boolean value after evaluation, it also has a custom string message option. If false then exception is raised and execution is terminated. The unused gas is returned back to the caller and the state is reversed to its original state. Following are some cases when require type of exception is triggered :
- When require() is called with the arguments which result as false.
- When a function called by a message does not end properly.
- When a contract is created using the new keyword and the process does not end properly.
- When a codeless contract is targeted to an external function.
- When ethers are sent to the contract using the public getter method.
- When .transfer() method fails.
- When an assert is called with a condition that results in false.
- When a zero-initialized variable of a function is called.
- When a large or a negative value is converted to an enum.
- When a value is divided or modulo by zero.
- When accessing an array in an index which is too big or negative.
Example: In the below example, the contract requireStatement demonstrates how to use the ‘require statement’.
Solidity
pragma solidity ^0.5.0;
contract requireStatement {
function checkInput(uint _input) public view returns(string memory){
require(_input >= 0, "invalid uint8" );
require(_input <= 255, "invalid uint8" );
return "Input is Uint8" ;
}
function Odd(uint _input) public view returns( bool ){
require(_input % 2 != 0);
return true ;
}
}
|
Output :

Assert Statement
Its syntax is similar to the require statement. It returns a boolean value after the evaluation of the condition. Based on the return value either the program will continue its execution or it will throw an exception. Instead of returning the unused gas, the assert statement consumes the entire gas supply and the state is then reversed to the original state. Assert is used to check the current state and function conditions before the execution of the contract. Below are some cases with assert type exceptions :
- When an assert is called with a condition that results in false.
- When a zero-initialized variable of a function is called.
- When a large or a negative value is converted to an enum.
- When a value is divided or modulo by zero.
- When accessing an array in an index which is too big or negative.
Example: In the below example, the contract assert Statement demonstrates how to use an ‘assert statement’.
Solidity
pragma solidity ^0.5.0;
contract assertStatement {
bool result;
function checkOverflow(uint _num1, uint _num2) public {
uint sum = _num1 + _num2;
assert (sum<=255);
result = true ;
}
function getResult() public view returns(string memory){
if (result == true ){
return "No Overflow" ;
}
else {
return "Overflow exist" ;
}
}
}
|
Output :

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: In the below example, the contract revertStatement demonstrates the ‘revert statement’.
Solidity
pragma solidity ^0.5.0;
contract revertStatement {
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);
}
}
}
}
|
Output :


Custom Error
To define the custom error in the smart contract by using the “Error statement” . Error statement is used either inside the smart contract or outside the smart contract along with revert statement. The main purpose of using custom error in the smart contract is to save gas.
Example : In the below example some amount is deposited to the owner address and if owner wants to transfer this amount to given address than first we checks withdraw amount is less than owner address amount if the condition is matched than withdraw amount is successfully transferred to the given address. Otherwise, If the condition is not matched than transaction is reverted with the help of Error statement provided by the contract.
Solidity
pragma solidity >=0.4.22 <0.9.0;
contract CustomError {
mapping(address => uint) public bal;
function deposit() public payable {
bal[msg.sender] += msg.value;
}
error LackOfFunds(uint withdrawAmt, uint availableAmt);
function checkCustomError(address _receiver,uint _withdrawAmt) public {
if (bal[msg.sender]< _withdrawAmt) {
revert LackOfFunds({withdrawAmt: _withdrawAmt, availableAmt: bal[msg.sender]});
}
bal[msg.sender] -= _withdrawAmt;
bal[_receiver]+=_withdrawAmt;
}
}
|
Output:
transact to CustomError.checkCustomError errored: VM error: revert.
revert
The transaction has been reverted to the initial state.
Error provided by the contract:
LackOfFunds
Parameters:
{
“withdrawAmt”: {
“value”: “200”
},
“availableAmt”: {
“value”: “100”
}
}
Debug the transaction to get more information.
.png)
Remix Output
.png)
Remix Console Output
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
27 Feb, 2023
Like Article
Save Article