Functions, Errors And Exceptions In Solidity


The main goal of Solidity is to create contracts. Writing a contract, however, necessitates sound error and exception handling. In programming, errors and exceptions are commonplace. Solidity offers a robust architecture for handling both. A good practice is to write solid contracts with good error and exception management.

Functions, Errors and Exceptions in Solidity.

Assert and Require

Convenience functions that check for conditions include assert and need. These throw exceptions when requirements are not satisfied. Before exceptions, need is used to check inputs and conditions. Code that should never be false is checked using the assert statement.

Transactions and Errors

  • Transactions are atomic. In other words, if the function fails for any reason, all of its operations are aborted. It's tested, and reported that the first transfer has been cancelled.
  • Errors are "state reverting." This implies that if an error is thrown, any modifications to the contract performed on that call or any sub-calls are reversed. A mistake is also reported.
  • Catching is not possible in Solidity. When Solidity is converted to byte code, a syntax error check is performed at that time. Runtime mistakes, however, are more challenging to detect, and typically arise when contracts are being executed.
  • Revert and require can return an error string.

Assert Vs Require

  • Revert operation (instruction 0Xfd) for require, which returns the remaining gas.
  • Invalid operation (instruction 0Xfe) for assert, which consumes all gas.
  • Assert is used to validate invariants.
  • Require is used to validate user input.

Assert is Triggered

  • When the smart contract is out of bounds index.
  • Division by zero or module zero.
  • Byte shifting by negative amount.
  • Convert a value to big or convert a value to enum.
  • Zero - iniatialized variable of internal function type.
  • Assert(x) where X is to false.

Require is Triggered

  • Require(x) where X is to false.
  • Function call via message call, but it won't finish properly.
  • External function call to contract does not containing any code.
  • Your contract receives ether without the payable modifier.
  • Your contract receives ether at a getter function.


  • This is outdated, and deprecated.
  • Remove in solidity 0.4.0.

Functions[Visibility, Constructor, Fallback]

Setter and Getter Functions

  • Writing transactions: transactions.
  • Reading transactions: call.
  • Calls are against a local blockchain node.
  • Remember: Everyone has a copy of the Blockchain. If you don't need to change anything, you don't need to inform other participants.
  • Reading is virtually free.

Function Visibility

  • PUBLIC: can be called internally and externally.
  • PRIVATE: only for the contract not externally reachable and not via derived contracts.
  • EXTERNAL: can be called from other contracts. can be called externally.
  • INTERNAL: only from the contract itself or from the derived contracts. It can't be invoked by transaction.


  • A function with the name "constructor()".
  • Called only once during deployment.
  • Can't be called afterwards.
  • Is either public or internal.

Fallback Function

  • A function without a name "function ()".
  • Called when a transaction without a function .call is sent to the smart contract.
  • Called when the in the transaction isn't found.
  • Can only be external.
  • Contracts receiving ether without a fallback function and without function call with throw an exception.

Next Recommended Reading Events In Solidity