Who can tell me what is the difference between revert,assert,require,throw!
1 Answer
Throw is the old mechanism for reverting the state of the current call (and all its sub-calls)
From version 0.4.13 the throw keyword is deprecated and will be phased out in the future.
Revert is the new, preferred mechanism for reverting a call
The revert function can be used to flag an error and revert the current call.
As for assert and require:
The convenience functions assert and require can be used to check for conditions
The difference between them being
Assert:
The assert function should only be used to test for internal errors and to check invariants
Require:
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
e.g.
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;
}
}