Error Handling in Solidity.
Error handling in solidity is used to take care of errors in your smart contract. There are four ways(Functions) that are used to handle errors in solidity. i. Require.
ii. Assert.
iii. Revert.
iv. Throw.
In this article, I'll be explaining what these error handlers do and their functions.
Require: This function declares the constraints which should be satisfied before executing the code. It accepts just one single argument and returns a boolean after evaluation. So after this function has evaluated the conditions, it returns a boolean(True or False) after its evaluation. Require uses all the gas fee if the evaluation turns out to be true and returns back the gas fee to the sender in the case that the evaluation turns out to be false. Also, the state is reverted back to the original state if its evaluation turns out to be false.
Assert: This is similar to require. It returns a boolean after evaluation of the condition. If the conditions are met, then it uses the gas fee. When the evaluation is true, the program will continue. This means that the program has been asserted to be true. In case of false, an exception will be thrown. Assert function error handler can be used to check for the current state and also function conditions before the execution of the contract.
Revert: Revert is used when an exception is thrown, unlike Assert that evaluates before an exception is thrown. It generates a string message that shows the details of the exception thrown. Unused gas is reverted from the message sent to the caller, and then it reverts the state to its original state.
Throw: This error handler consumes all the gas, it doesn't matter if there were errors, the gas is completely consumed.
This is just a basic explanation of error handling for solidity. check out the links below for more of the resources. (tutorialspoint.com/solidity/solidity_error_..) (geeksforgeeks.org/solidity-error-handling)
Don't forget to read the Docs: docs.soliditylang.org/en/v0.7.4
For more on Topics on Solidity, follow me on my 100DaysOfCode #100DaysOfSolidity challenge on Twitter