An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
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 functions assert
and require
can be used to check for conditions and throw an exception if the condition is not met.
throw
has been deprecated in the latest versions of Solidity, and should instead be replaced by require()
for checks on externally provided input data, and assert()
for checks on internal conditions that should never achieve a certain state.
pragma solidity ^0.4.0;
contract Sharer {
function sendHalf(address addr) payable returns (uint balance) {
require(msg.value % 2 == 0); // Only allow even numbers
uint balanceBeforeTransfer = this.balance;
addr.transfer(msg.value / 2);
// Since transfer throws an exception on failure and
// cannot call back here, there should be no way for us to
// still have half of the money.
assert(this.balance == balanceBeforeTransfer - msg.value / 2);
return this.balance;
}
}