Loading...
While learning solidity many of you must have come across error handling in solidity. First, let's see what is meant by error handling.
So error handling as the name suggests is the process of identifying, reporting, and responding to the error that occurs during the execution of the program. As solidity is just another programming language it's also got the error handling thing.
So how does this error handling work in a solidity programming language?
Well, we can do this using the following methods -
let's see this one by one -
function divide(uint256 a, uint256 b) public view returns (uint256) {
require(b != 0, "division by zero");
return a / b;
}
So in the above function, the "require" condition checks if the "b" value is equal to the "zero" or not, if b is equal to zero that means our condition is failed and the following code will not execute. In order to get the result a/b we have put the minimum condition that b should not be equal to zero that's it!
So mainly the "require" condition is used for input validation check just like we did in the above case.
function withdraw(uint256 amount) public {
require(balance[msg.sender] >= amount, "insufficient balance");
if (!msg.sender.send(amount)) {
revert("transfer failed");
}
balance[msg.sender] -= amount;
}
So in the above function, we have saved the user's balance in the mapping balance, and when the user withdraws the amount we check if the current balance is greater than the withdrawal amount using require.
Then below that, we are using the if condition - if the amount transfer is failed then revert back. Like, require revert will also reverse the state change. But mainly revert is used for internal errors like the above example. We can also use revert for input validation no doubt about that.
function add(uint256 a, uint256 b) public pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
It is used to check for code errors, such as integer overflows or underflows. If the condition evaluates to false, the contract will immediately stop executing and any state changes made so far will be rolled back. Unlike require and revert, assert is not used for checking user inputs or enforcing preconditions. In the above example, the assert checks if the integer has overflowed or not.
Here are some general guidelines for selecting error-handling methods in your Solidity smart contract:
- Use require when you need to validate inputs, preconditions, or invariants that must be true for the function to execute correctly. require is commonly used to check for valid inputs, valid state, and authorization requirements.
- Use revert when an error occurs that is not caused by an external call, such as an out-of-gas condition or a failed internal assertion. revert is often used to roll back state changes and provide a meaningful error message to the user.
- Use assert when an internal error occurs that should never happen if the program is correct. assert is typically used to check for bugs, invariant violations, and other conditions that indicate a programming error.
Happy Coding! 🤙