89DEVs

Error Handling in Solidity

error handling in solidity

There are three ways to throw an error in Solidity: require, revert and assert.

require is mostly used to validate input values and for access control in functions. The revert statement doesn't evaluate anything and is because of that mostly used within if statements. The assert statement checks for values that should always be true and if they are not truly an error is thrown.

Since Solidity version 0.8 it is possible to throw custom errors. The gas cost of a custom error depends on the error message, therefore a custom error can be used to save gas costs and optimize gas consumption of the solidity contract.

three ways to throw an error

There are three ways to throw an error in Solidity:
  1. require
  2. revert
  3. assert

require

The require statement checks if a condition is true and is mostly used for input validation. For example, we can make sure input is in a specific range or of a specific type. // SPDX-License-Identifier: MIT pragma solidity ^0.8; // Error Handling in Solidity: require contract ErrorHandling { function testRequire(uint n) public pure { require (n < 89, "n is not smaller than 89"); } } Here, the require statement checks if the input n is smaller than 89. If it is true, the rest of the contract code is executed. If not an error is thrown an the state is reverted.

revert

The revert statement doesn't evaluate any condition and is therefore used in combination with some other logic like an if/else statement. // SPDX-License-Identifier: MIT pragma solidity ^0.8; // Error Handling in Solidity: revert contract ErrorHandling { function testRevert(uint n) public pure { if (n > 89) { revert("n is not smaller than 89"); } } } The code here does exactly the same as the code shown for the require statement.

assert

The assert statement is used to check for conditions that should always be true. For example, we can verify that a state variable is not updated accidentally. // SPDX-License-Identifier: MIT pragma solidity ^0.8; // Error Handling in Solidity: assert contract ErrorHandling { uint public n = 89; function testAssert() public view { assert(n == 89); } } The state variable n is assigned the value 89. The assert statement checks if the statement is true. If it is true, the rest of the contract is executed, if it is not true then an error is raised. In this case, we are reading a state variable, therefore the function testAssert() is set to public view.

custom error

Customer errors are available since solidity version 0.8 and are used to safe gas. The main advantage is that a custom error allows setting a customized error message. The gas costs are depending on the length of the error message and therefore custom errors are a good way to save gas costs in Solidity. However, custom errors can only be used with the revert statement. // SPDX-License-Identifier: MIT pragma solidity ^0.8; // Error Handling in Solidity: custom error contract CustomError { error MyCustomError(address caller); function testCustomError(uint n) public view { if (n > 89){ revert MyCustomError(msg.sender); } } } Here the error MyCustomError is defined and the caller address is used as a custom error message. The execution costs for this contract were 21,850 wei, while the revert contract above costs us 21,901 gas.

gas consumption and refund

In case an error is thrown, the remaining gas will be refunded. Example: 10,000 gas is sent and the contract consumed 1,000 gas before the error occurred, then the remaining 9,000 gas will be refunded.

state variables are reverted

All state variables that are updated before the error occurred, are reverted to the original values. For example, if a state variable is increased within a function before an require statement then it is reverted back to the value at the start.

        

Summary

Click to jump to section