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,370 points
2,315 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,220 points

Related Questions In Blockchain

0 votes
2 answers

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

Here's a simple example for web3js 1.0.0.beta*: function handler ...READ MORE

answered Jun 8, 2018 in Blockchain by aryya
• 7,460 points
2,496 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 25, 2018 in Blockchain by Christine
• 15,790 points
7,540 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,790 points
2,060 views
0 votes
1 answer

How to swap an Ethereum ERC-20 and Neo NEP5 token with a smart contract?

Each blockchain is its own separated administration. ...READ MORE

answered Apr 12, 2022 in Blockchain by Soham
• 9,710 points
879 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,790 points
2,045 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,575 views
+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
1,715 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 10, 2018 in Blockchain by Shashank
• 10,400 points
16,078 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,220 points
5,189 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP