Error Handling in Solidity

Solidity uses state-reverting exceptions to handle errors. Such an exception will undo all changes made to the state in the current call(and all its sub-calls) and also flag an error to the caller. The convenience function assert and require can be used to check for conditions and throw an exception if the condition is not met.

The assert function should only be used to test for internal errors, and to check invariants.

The require function should be used to ensure valid conditions, such as inputs, or contract state variables are met, or to validate return values from calls to external contracts.

If used properly, analysis tools can evaluate your contract to identify the conditions and function calls which will reach a failing assert. Properly functioning code should never reach a failing assert statement. If this happens there is a bug in your contract which you should fix.

There are two ways to trigger exceptions:

  • The revert function can be used to flag an error and revert the current call. In the future, it might be possible to also include details about error in a call to revert.

  • The throw keyword can also be used as an alternative to revert()

From 0.4.13, the throw is deprecated.

When exceptions happen in a sub-call, they ‘bubble up’ automatically. Exceptions to this rule are send and the low-level functions call, delegatecall and callcode – those return false in case of an exception instead of ‘bubble up’.

low-level function call, delegatecall, callcode return false when exceptions occur.

Catch exceptions is not yet possible.

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×