Skip to main content
deleted 8 characters in body
Source Link
alper
  • 8.4k
  • 11
  • 67
  • 155

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;
    }
}
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;
    }
}

Source: Solidity documentation

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;
    }
}

Source: Solidity documentation

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;
    }
}

Source: Solidity documentation

Source Link
SteveJaxon
  • 2.5k
  • 1
  • 11
  • 21

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;
    }
}

Source: Solidity documentation