2017-06-27 9 views
0

は、これは私の簡単な契約ですエテリアム契約例外の捕捉方法は?

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]; 
     } 
} 

私は別のintial供給よりも多くのトークンを転送していますtransferがすべき例外をスローする機能を占めています。

どのように私はこの例外を処理し、トランザクションがcomplete.I web3jを使用して、私はweb3jsを使用したことがないが、あなたはのtry-catchを使用して試すことができます

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

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

答えて

0

のような機能の移転を呼び出していないことを知ってもらうことができます。

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 
} 
+0

私はすでにこれを試しましたが、何も例外はありません... !! –

0

どのように私はこの例外を処理し、トランザクションを知ることができない 完全

"Out of gas"という単一性例外(引数なしのthrow)があります。あなたの "間違った"取引ですが、ガスが不足しています。トランザクションハッシュを知っている場合は、gasLimitgasUsedをチェックすることができます。両者が等しい場合、おそらく*の取引にガスがなくなっている可能性があります。詳細はhereを参照してください。

* "正しい"取引に必要なガス以上のガスを供給した場合、

+0

ありがとう@jeff、しかし私は例外を探しているのではなく、これを解決しました。私は、アカウントからの残高を最初にチェックしました。 –

+0

その場合、ほとんどのトランザクション署名ライブラリは、トランザクションを送信しなくても「資金不足」というエラーを表示します。しかし、はい、それは重要な点検です。あなたの問題が解決してうれしいです! – jeff

+0

ええ、実際に私は残高を別の口座に振り替える前に口座から残高を確認しています。 –

関連する問題