127 votes
Accepted

Difference between require and assert and the difference between revert and throw

edit 2022-12-08: As of Solidity 0.8.0 this answer is no longer accurate with respect to opcode and gas usage. Furthermore, internal errors like division by zero, assertion failure and others do not ...
maurelian's user avatar
  • 3,211
29 votes
Accepted

Is it a good practice to log an event every time I throw in Solidity?

With an exception, as with throw, all effects (including events) of a transaction are reverted, except for the payment to the miner. So there would be no benefit in firing an event before a throw. ...
eth's user avatar
  • 85.7k
26 votes

How to map EVM trace to contract source?

I went down this rabbit hole and got a proof of concept to work at the end. I can not recommend the journey. There's impedance mismatches on many levels, requiring lots of format conversions. In the ...
Remco's user avatar
  • 413
25 votes

Difference between require and assert and the difference between revert and throw

I'm using require for input validation as it's a little more efficient that if/throw. function foo(uint amount) { require(amount < totalAmount); ... } Where as assert should be used more ...
o0ragman0o's user avatar
  • 4,320
23 votes
Accepted

Why does a Solidity throw consume all gas?

All gas is consumed because the EVM essentially only has 1 exception: Out of Gas. To see this clearer, take a look at the difference between a "pure" exception, and an error due to bad/buggy/invalid ...
eth's user avatar
  • 85.7k
20 votes
Accepted

Does an entire transaction revert when throw occurs?

In Solidity, by default, yes. On the EVM-level, a throw (bad jump, out-of-gas, or any other exception) only reverts the call it is inside. Solidity helpfully continues the exception down the stack ...
Matthew Schmidt's user avatar
19 votes

Difference between require and assert and the difference between revert and throw

I think none of the answers is correct. assert is reserved for conditions in which it is expected that static code analysis tools (maybe Solidity compiler in future versions) will be able to detect ...
earizon's user avatar
  • 606
15 votes

Does an entire transaction revert when throw occurs?

To complement @Matthew's answer, it depends on how the call is made in Solidity. If C calls D.foo(), and foo does a throw, then yes the entire transaction is reverted. If C does a "lower-level raw ...
eth's user avatar
  • 85.7k
15 votes
Accepted

Why do `throw` and `revert()` create different bytecodes?

I'll leave the precise interpretation of the OpCodes to someone else and just point out that they are different instructions so different implementations are to be expected. According to this: http:/...
Rob Hitchens's user avatar
  • 55.2k
11 votes

Why does a Solidity throw consume all gas?

In part I believe this was a security decision to ensure spamming the network with bogus transactions cost the sender gas. For instance, if gas was refunded after a throw, a malicious actor could ...
Robert McCone's user avatar
11 votes

Why does a Solidity throw consume all gas?

The Solidity throw instruction gets compiled to an invalid JUMP instruction (i.e. a jump to an invalid location). The section 9.4.2 of the Yellow Paper explains how exceptional halting works, in ...
axic's user avatar
  • 2,569
10 votes

Throw vs. Return

All the considerations in the question are helpful. throw is safer since it ensures that there are no side effects that remain, but another consideration is error reporting. With throw there's ...
eth's user avatar
  • 85.7k
7 votes

Why does a Solidity throw consume all gas?

The throw instruction is a EVM hack that reverts all changes made by current contract execution. It exploits exceptional termination of EVM which also consumes all gas.
Paweł Bylica's user avatar
6 votes
Accepted

Does `throw` refund the ether value?

The ether value that was sent along with the transaction (or call if the exception is caught) is returned to the sender if you use throw in Solidity.
chriseth's user avatar
  • 3,392
6 votes

Is there an advantage rollbacking the transactions with 'throw' over returning an error code?

For example with web3js you would have to guestimate if the full consumtion of gas means that a throw happend or you ran out of gas Guesstimation is unnecessary, you can get the estimated gas cost ...
cryptihkal's user avatar
6 votes
Accepted

Exception When Calling Function In a Library that Returns an Array

I've never worked with libraries before, however I believe the issue might be that you are trying to have a dynamically sized array in memory. This should be uint8[] memory arr = new uint8[](arraySize)...
phaze's user avatar
  • 1,585
5 votes
Accepted

Custom Exceptions in Solidity

Currently there are no custom exceptions in Ethereum Virtual Machine code. All failure conditions are "out of gas" as exception is modelled as consuming all the remaining gas. eth.debug....
Mikko Ohtamaa's user avatar
5 votes

Does `throw` refund the ether value?

Yes, all funds are returned to the sender, except the gas. https://github.com/ethereum/wiki/wiki/Solidity-Features#throw
Loi.Luu's user avatar
  • 2,103
5 votes

Running contract CrowdFunder in Mix and function modifier causes - Solidity exception (bad jump)

"bad jump" is just the way "throw" is modeled, so it is expected behaviour.
chriseth's user avatar
  • 3,392
5 votes

Would it be better to use `throw` instead of `return false`?

Just thought I would chime in, in case this Q&A is useful to others. For brevity, just imagine I prefixed everything with in my opinion. It's a very general question. I would segregate the ...
Rob Hitchens's user avatar
  • 55.2k
5 votes

Why do `throw` and `revert()` create different bytecodes?

The revert is often referred to as cheap throw as it refunds unused gas to the sender. If you are interested in the detailed design of this feature please look at the original EIP-140 discussion.
Jakub Wojciechowski's user avatar
4 votes
Accepted

Buy function not working with solidity 0.4.2

From Solidity 0.4 onwards: Functions that want to receive Ether have to specify the new payable modifier (otherwise they throw). A throw consumes all gas, so function buy() payable returns (uint ...
eth's user avatar
  • 85.7k
4 votes
Accepted

Throw not generated when accessing mapping with invalid key

In soldidity mappings exist as if all keys were present with their values set to 0x0. For structs this means that all of the fields will be in their 0x0 state. Since voters is a mapping, you can ...
Piper Merriam's user avatar
4 votes

Throw vs. Return

You may also want to consider why not to use throw / return: Why not use throw All gas is consumed, everything reverted Impossible to tell callers why the call failed Errors cannot be caught, so ...
Ether Dude's user avatar
4 votes
Accepted

Not enough components (4) in value to assign all variables (6)

It turns out that in solidity, you can't go passing around strings from contract to contract. When I removed the two string values my code compiled without a hitch.
unflores's user avatar
  • 201
4 votes
Accepted

Contract created by another contract and exceptions

1. Yes B will not be created if the transaction containing the creation of B has an exception. Example: pragma solidity ^0.4.8; contract A { B public b; function foo() { b = new B()...
eth's user avatar
  • 85.7k
4 votes

simulate transaction without sending to blockchain

I am a software engineer at Tenderly and our Transaction Simulator is pretty much the solution for the problem you described above and our most used feature. You can: Test out bug fixes and ...
dzimiks's user avatar
  • 141
3 votes
Accepted

Logging errors in the fallback function

Since I have only 2300 gas available I can't call another contract that will make a log. Is it correct? The basic cost for calling another contract is 700 gas (cost of CALL opcode). So it's ...
eth's user avatar
  • 85.7k
3 votes
Accepted

Running contract CrowdFunder in Mix and function modifier causes - Solidity exception (bad jump)

The modifier inState(State _state) is basically an assertion insuring that before the actual function runs (represented by the _ in the modifier's code), the current state of the contract is in the ...
Thomas Jay Rush's user avatar
3 votes

Is there an advantage rollbacking the transactions with 'throw' over returning an error code?

Throwing an exception makes it easier to reason about the resulting state on error. It's too easy to accidentally not revert all changes that were made on error if you return a value. Future versions ...
Nikolai's user avatar
  • 33

Only top scored, non community-wiki answers of a minimum length are eligible