7

In solidity, is there any way to throw custom Exceptions along with error messages? I am using throw to prevent further execution of my code but this always results in invalid JUMP(in geth console, on debugging) and Intrinsic gas too low (in Mist).Can I have custom error messages?
For example can we modify transfer(..) function to produce error message account balance is low when balance of sender is less than amount he wants to transfer?

 function transfer(address _to, uint256 _value) {

    if (balanceOf[msg.sender] < _value)        // Check if the sender has enough
{
        throw;  
        // some code to display  "account balance is low" to  user instead of 'Intrinsic Gas too low'
}

    balanceOf[msg.sender] -= _value;                     // Subtract from the sender
    balanceOf[_to] += _value;                            // Add the same to the recipient
    Transfer(msg.sender, _to, _value);                // Notify anyone listening that this transfer took  

}
4

Currently there are no custom exceptions in Ethereum Virtual Machine code. All failure conditions are "out of gas" as exception is modelled as consuming all the remaining gas.

eth.debug.traceTransaction API may give you some insight, but it doesn't implement human readable error mechanisms at the moment as far as I know.

  • 1
    Technically, throw results in an "Invalid Jump" error, not an out of gas error, but the result is the same. – Tjaden Hess Aug 11 '16 at 0:13
  • 3
    That's strange. I mean whatever be the reason of failure of transaction user will always get Invalid Jump. Becomes quite unhandy to our users. – Prashant Prabhakar Singh Aug 11 '16 at 8:57
  • @PrashantPrabhakarSingh: This is a known issue. The original protocol was developed simplicity in mind and I believe the future Ethereum versions will address this. – Mikko Ohtamaa Aug 11 '16 at 16:17
0

From solidity 0.4.22, It is possible to add error messages with require and assert. throw is deprecated. It took long time but finally adding an error statement is possible. Refrence

A sample contract using this is illustrated as:

pragma solidity ^0.4.23;
contract TestExceptionHandling{
    uint public a ;
    constructor(uint _a) public{
        a= _a;
    }
    function increaseA(uint b) public{
        require(b > a, 'new value must be greater than a');
        if (b > 50){
            revert('Very large value');
        }
        a = b;
    }
}

I am not sure of How to catch this error message in DAPP and show the error message to UI. Will update the answer when I have explored that.

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.