8

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  

}

3 Answers 3

5

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.

4
  • 1
    Technically, throw results in an "Invalid Jump" error, not an out of gas error, but the result is the same. Commented Aug 11, 2016 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. Commented Aug 11, 2016 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. Commented Aug 11, 2016 at 16:17
  • Please note that this is 2016 answer. Commented Apr 26, 2021 at 8:00
1

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.

1

As of now you can throw an error like this. I am not sure when it was added:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

/// Write custom Error!
error MyCustomError();

It is 3 "///". "Write custom Error!" is the message that will be shown to the user

then call it in your function

   if (thisIsNotSatisfied){
          revert MyCustomError()
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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