Web3js how to catch ethereum contract exception

0 votes

This is my simple contract

contract Test {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function Test(
        uint256 initialSupply
        ) {
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
    }

    /* Send coins */
    function transfer(address _to, uint256 _value) {
        if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[msg.sender] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
    }

function gettokenBalance(address to)constant returns (uint256){
          return balanceOf[to];
       }
}

When i am transferring tokens more than intial supply to another account the function transfershould throws exception.

How can i handle this exception and get to know transaction cannot complete.I am using web3j and calling function transfer like

Test test = Test.load(contractObj.getContractAddress(), web3j, credentials, gasprice,gaslimit);

TransactionReceipt balanceOf = test.transfer(new Address(address), transferBalance).get(); 

Oct 22, 2018 in Blockchain by slayer
• 29,300 points
469 views

1 answer to this question.

0 votes

try this:

try{
  Test test = Test.load(contractObj.getContractAddress(), web3j, credentials, gasprice,gaslimit);

  TransactionReceipt balanceOf = test.transfer(new Address(address), transferBalance).get(); 
} catch (Exception e){
  // log you exception
}
answered Oct 22, 2018 by Omkar
• 69,110 points

Related Questions In Blockchain

0 votes
2 answers

How to get notified when an event triggers on ethereum smart contract?

Muchas gracias. ?Como puedo iniciar sesion? READ MORE

answered May 2, 2020 in Blockchain by aqowcmbevs
1,075 views
0 votes
1 answer

How to detect if an ethereum address is an ERC20 token contract?

There are many possible ways to achieve ...READ MORE

answered Sep 24, 2018 in Blockchain by Christine
• 15,830 points
2,420 views
0 votes
1 answer

How to get ethereum contract public variables?

You need to use call method, it ...READ MORE

answered Oct 17, 2018 in Blockchain by Christine
• 15,830 points
176 views
0 votes
1 answer

How to make sure transactions take no fee in a private Ethereum blockchain?

In a private ethereum network you have ...READ MORE

answered Mar 26, 2018 in Blockchain by Christine
• 15,830 points

edited Mar 26, 2018 by Christine 583 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,830 points
555 views
0 votes
1 answer
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
381 views
+1 vote
4 answers

How to estimate the cost for deploying smart contract on mainnet?

Since you have already deployed the contract ...READ MORE

answered Apr 9, 2018 in Blockchain by Shashank
• 10,400 points
5,313 views
0 votes
2 answers

How to generate ethereum qr code address with amount?

You implement this package: ethereum-qr-code. It implements the EIP67 standard which uses ...READ MORE

answered Sep 6, 2018 in Blockchain by Omkar
• 69,110 points
1,928 views