- require() is a convenience function used to check conditions and throw an exception if a condition is not met.
- Earlier versions of solidity (prior to 0.4.13) used the throw pattern to revert the state during an exception. It is deprecated now and will be removed in a future version:
// Older and deprecated method
if (msg.sender != owner) {
throw;
}
- Use the require() method to ensure conditions such as inputs are valid, or to validate return values from calls to external contracts:
require(toAddress != address(0));
require(targetContract.send(amountInWei));
require(balances[msg.sender] >= amount);
- Return an error message or a number corresponding to an error type using require():
require(condition, "Error Message");
- The function will refund the remaining gas to the caller if an exception occurs.
- require() uses the 0xfd opcode to cause an error condition. The 0xfd opcode is currently (EIP-140) mapped to the REVERT instruction.
Ethereum Improvement Proposal (EIP) 140 introduces a new opcode called REVERT. The proposal is to create a REVERT opcode that would throw an exception without draining all the gas that is allocated by the caller. You can get more information here: https://github.com/ethereum/EIPs/issues/140.