assert()

  1. Use assert() to prevent something bad from happening:
assert(condition);
  1. Use it to check conditions such as integer overflow/underflow:
pragma solidity ^0.4.24;

contract TestException {
function add(uint _a, uint _b) returns (uint) {
uint x = _a + _b;
assert(x > _b);
returns x;
}
}
You should not use assert() blindly for checking overflow/underflow. It should be used only if you think that previous require or if checks would make it impossible.
  1. Use it to check invariants in the contract. One example is to verify the contract balance against the total supply:
assert(address(this).balance >= totalSupply);
  1. assert is commonly used to validate state after making changes. It helps in preventing conditions that should never be possible. In an ideal scenario, the contract should never reach a failing assert statement. If this happens, there is a bug in your contract that you should fix.
  2. Generally, use assert towards the end of your function and use it less often.
  3. assert() uses the 0xfe opcode to cause an error condition if something fails. 
  4. The opcode will consume all available gas for the call and revert the state changes made before the exception.
..................Content has been hidden....................

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