Error handling in solidity

Error handling in solidity

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.

  • require
  • assert
  • revert
  • throw

require

  • 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)

Example require(newBalance > minimumBalance, "Your do not have minimum balance in your account")

 

assert

  • Prevent anything really bad from happening.
  • This does consume all the gas and revert the state
Syntax assert(condition)

Example assert(balance %2 == 0)

 

revert

  • This revert the state along with error message
Syntax revert("failure reason")

Example revert("you do not have sufficient balance" )

 

throw

  • This is used to revert the state but this is depreciated now
Syntax throw

Example throw;

No Comments

Post a Reply

Inquire Now
close slider