1

It seems like the usual way of handling a failed send in the Solidity docs is to use throw. I was confused when in the withdraw function of the blind auction example they don't use throw and instead manually set the state back to before the call was made. I was wondering why they decided not to use throw here?

        if (!msg.sender.send(amount)){
            // No need to call throw here, just reset the amount owing
            pendingReturns[msg.sender] = amount;
            return false;
        }
1
  • throw is generally considered "cleaner" in that the state is guaranteed to be rolled back as if the transaction never happened, so most developers choose to use that method. The issue is that this consumes all of the transaction's gas, and it is hard for other contracts to handle the exception gracefully. So in cases where only very simple state changes are made, it can be better to just roll back the changes and exit gracefully. – Tjaden Hess Mar 27 '17 at 19:58
2

When you use throw during the execution of a function, all changes in the state are reversed and all gas sent by the sender is consumed.

In that function, as it is not modifying other variables of the state, it is enough to reset that variable to its initial value.

They are probably doing it this way instead of using throw to avoid wasting the sender's gas.

3
  • Doesn't this leave the contract susceptible to ddos? – Daniel Kobe Mar 28 '17 at 6:03
  • What do you mean by ddos in this case? the sender still needs to spend some gas at market price in order to execute the function. – AdrianClv Mar 28 '17 at 8:24
  • I thought the fact that throw uses all gas is supposed to provide additional protection – Daniel Kobe Mar 28 '17 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.