[Ethereum] Warning: “throw” is deprecated in favour of “revert()”, “require()” and “assert()”. throw;

erc-20etherscanremixsolidity

my question is if "THROW" is deprecated then what i have to put instead throw in the following solidity code ?

// This function creates Tokens  
     function createTokens() payable {
        if(isMinting == true){
            require(msg.value > 0);
            uint256  tokens = msg.value.div(100000000000000).mul(RATE);
            balances[msg.sender] = balances[msg.sender].add(tokens);
            _totalSupply = _totalSupply.add(tokens);
            owner.transfer(msg.value);
        }
        else{
            throw;
        }
    }

so the code should look like this:

// This function creates Tokens  
 function createTokens() payable {
 require(isMinting == true && msg.value > 0);
 uint256  tokens = msg.value.div(100000000000000).mul(RATE);
 balances[msg.sender] = balances[msg.sender].add(tokens);
 _totalSupply = _totalSupply.add(tokens);
 owner.transfer(msg.value);
    }
    else{
        throw;
    }
}

Or i have to delete the

else{
throw;

last update:

function createTokens() payable public {
    if(isMinting == true){
       require(isMinting, "Not allowed");
       require(msg.value > 0, "No funds");
        uint256  tokens = msg.value.div(100000000000000).mul(RATE);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        _totalSupply = _totalSupply.add(tokens);
        owner.transfer(msg.value);
    }
    else{
        revert();
    }

thanks AK

Best Answer

If you are looking for an alternative of throw, you can use revert in the similar way. The revert function can be used to flag an error and revert the current call. As stated in solidity docs :

Note

There used to be a keyword called throw with the same semantics as revert() which was deprecated in version 0.4.13 and removed in version 0.5.0.

From code point of view, you can use the require as below:

 function createTokens() payable {
        require(isMinting == true && msg.value > 0);
        uint256  tokens = msg.value.div(100000000000000).mul(RATE);
        balances[msg.sender] = balances[msg.sender].add(tokens);
        _totalSupply = _totalSupply.add(tokens);
        owner.transfer(msg.value);
  }

So if isMinting is not equal to true, it will throw straight from there without making any changes in the state.

For more : https://solidity.readthedocs.io/en/v0.5.0/control-structures.html#error-handling-assert-require-revert-and-exceptions

Related Topic