3

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.

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.

  • 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 9: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
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 5: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
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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