require()

  1. require() is a convenience function used to check conditions and throw an exception if a condition is not met.
  2. 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;
}
  1. 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);
  1. Return an error message or a number corresponding to an error type using require():
require(condition, "Error Message");
  1. The function will refund the remaining gas to the caller if an exception occurs.
  2. 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.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.8