4

As per my knowledge,there are no custom exceptions in solidity. The only exception is Out of Gas. Wherever we write throw in contract code, it throws out-of-gas exception.
But, is there any way I can catch that exception? I mean if I can execute a piece of code whenever an exception occurs? Even on working with geth console, I didn't get any notification that whether any exception occurred.
I think it's pretty possible to catch such exception, because Mist is able to display Intrinsic gas too low error message, whenever an exception occurred, so it must have handled that exception somewhere.
Edit:
Basically the question is:

How can we know, before sending the transaction, that this transaction will consume all the provided gas?

I am not asking on How can I provide sufficient gas for my transaction but How can I know that my transaction will run out of gas.
One way of doing so is to send the transaction, let it be mined and then check for gasUsed=gasSend, of if there is thow use debug.traceTransaction.

But I want to know whether my transaction will go through or not without sending the transaction. In Mist whenever I try to send a transaction that will encounter a throw statement, It displays Intrinsic gas too low before sending the transaction. I want to do the same from geth.

Prashant Prabhakar Singh
  • 8,026
  • 5
  • 40
  • 79

2 Answers2

2

Solidity didn't offer an exception management mechanism as try/catch (for Java/c#).

due to an invalid EVM code, throw consumes all provided gas terminates and reverts all changes to the state made by current contract execution and to Ether balances. this concept intends to prevent spamming the network.

in the Yellow paper it is mentioned that :

Just as with contract creation, if the execution halts in an exceptional fashion (i.e. due to an exhausted gas supply, stack underflow, invalid jump destination or invalid instruction), then no gas is refunded to the caller and the state is reverted to the point immediately prior to balance transfer (i.e. σ)

as an information for the “intrinsic gas too low” message

Mist checks the amount gas without executing any code and it tells you that the transaction will fail because you didn't provide enough gas to execute the transaction.

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
  • I know the facts you mentioned, but this doesn't solve my problem. Probably you got me wrong. I was asking about catching the out-of-gas exception, so that I can display a message like Mist does. – Prashant Prabhakar Singh Sep 19 '16 at 09:59
  • that's what i mean by try/catch mechanism, solidity didn't provide that. – Badr Bellaj Sep 19 '16 at 10:01
  • you could use events when you expect an exception will be fired – Badr Bellaj Sep 19 '16 at 10:02
  • I was wondering if there is no such mechanism, How Mist is able to display an error message whenever an exception occurs – Prashant Prabhakar Singh Sep 19 '16 at 10:16
  • Intrinsic gas too low this is not an exception MIST just check the gas amount then gives you this error – Badr Bellaj Sep 19 '16 at 10:38
  • check Intrinsic http://ethereum.stackexchange.com/questions/1570/mist-what-does-intrinsic-gas-too-low-mean – Badr Bellaj Sep 19 '16 at 10:38
  • See, no matter how much gas you provide, whenever we throw an exception, all gas is consumed and hence Mist shows Intrinsic gas too low. But, if you perform the same transaction from geth console, the transactions is send, but after mining when you debug it, it will give invalid jump destination (PUSH1) 2. What i want is, I can prevent transaction from being send just as Mist does, if there is somewhere exceptions s thrown, or the transaction consumes all gas. – Prashant Prabhakar Singh Sep 19 '16 at 12:01
1

If you want to estimate gas like Mist does, you can call this eth_estimateGas:

Makes a call or transaction, which won't be added to the blockchain and returns the used gas, which can be used for estimating the used gas.

  • yes, this is the only solution, but how can I call contract method using web3.eth.call. I mean , how to get data parameter, if I want to call a function like transfer("to_addr", amount, { from: "accnt_addr}); using web3.eth.call(). – Prashant Prabhakar Singh Sep 20 '16 at 05:48
  • eth_estimateGas will give me estimated amount of gas, but how will I coome to know, that my transaction consumed all the Gas? – Prashant Prabhakar Singh Sep 20 '16 at 11:12
  • When you do the .call(param, { gas:3000000 }), will it estimate the gas at 3000000? 3000000 is a lot. – Xavier Leprêtre B9lab Sep 20 '16 at 11:49
  • yeah, but what if function I am invoking uses throw at some line? It, will consume all gas, no matter how large the value is. So,if I want to know whether my code throws at any line or not, the best way to do this, I guess, is to check whether the transaction ran out of gas.I am not asking on how can I provide sufficient gas for my transaction but How can I know beforehand that my transaction will run out of gas? – Prashant Prabhakar Singh Sep 20 '16 at 11:57
  • I have updated the question, hope this will help to understand the case better. – Prashant Prabhakar Singh Sep 20 '16 at 12:10
  • If you give gas of 3000000, or 4700000, depending on what is the block's max gas, you give it the maximum possible gas to your transaction. So if it throws it consumes all the gas, however large the value. But because you gave the maximum value, you know it threw. – Xavier Leprêtre B9lab Sep 20 '16 at 13:12
  • I am not talking about sending the transaction from Mist, but from geth console. In that case, how shall I know, my transaction may fail before sending the transaction? – Prashant Prabhakar Singh Sep 20 '16 at 13:18
  • 1
    eth.estimateGas({ from: account, to: myContract.address, data: myContract.myFunction.getData(param1, param2) }) is available in geth console. – Xavier Leprêtre B9lab Sep 20 '16 at 13:58
  • Great, now I got it, actually whenever a transactions throws the Estimated gas=50000000. So, I can put a check like if(EstimatedGas=50000000){console.log("transaction may fail")}; . But this arose a new question of why 50000000? Is it max gas limit of Ethereum-transaction? – Prashant Prabhakar Singh Sep 21 '16 at 03:32
  • The term you need to Google is "block gas limit". – Xavier Leprêtre B9lab Sep 21 '16 at 10:52
  • Thanks for help. I am now stuck at using eth_estimateGas for functions that don't take any input parameter. To avoid discussions here, I opened a new question How to estimate gas for a function without any input parameter?. Please have a look. – Prashant Prabhakar Singh Sep 21 '16 at 11:32