Exceptions
In some cases, exceptions are thrown automatically. You can use assert()
, revert()
, and require()
to throw manual exceptions. Exceptions stop and revert any currently-executing calls (that is, all changes to the state and balances are undone). In Solidity, it is not yet possible to catch exceptions.
The following three lines are all different ways of throwing exceptions in Solidity:
if(x != y) { revert(); } //In assert() and require(), the conditional statement is an inversion //to "if" block's condition, switching the comparison operator != to == assert(x == y); require(x == y);
assert()
will take away all the gas, whereas require()
and revert()
will refund the remaining gas.Â
Note
Solidity doesn't support returning a reason for exceptions but is expected to soon. You can visit the https://github.com/ethereum/solidity/issues/1686 issue for an update. Then you will be able to write revert("Something bad happened")
 and require(condition, "Something bad happened")
.