Error handling is always a very important topic in any language, sample applies to solidity as well. Still it does not have all the features like handling of error and expected to be there in upcoming releases but there are few concepts that each developer need to understand before they deep dive in development using solidity.
There are four main concepts in solidity error handling.
- Here we check the conditions and revert the state in case condition does not met.
- This is most commonly used in solidity for error handling
- This does not consume any gas
Syntax require(condition, errorMessage)
require(newBalance > minimumBalance, "Your do not have minimum balance in your account")
- Prevent anything really bad from happening.
- This does consume all the gas and revert the state
assert(balance %2 == 0)
- This revert the state along with error message
Syntax revert("failure reason")
revert("you do not have sufficient balance" )
- This is used to revert the state but this is depreciated now