86 - Exceptions
Solidity uses state-reverting exceptions to handle errors. Such an exception undoes all changes made to the state in the current call (and all its sub-calls) and flags an error to the caller
-
When exceptions happen in a sub-call, they “bubble up” (i.e., exceptions are rethrown) automatically. Exceptions to this rule are send and the low-level functions
call
,delegatecall
andstaticcall
: they return false as their first return value in case of an exception instead of “bubbling up”. -
Exceptions in external calls can be caught with the
try/catch
statement -
Exceptions can contain data that is passed back to the caller. This data consists of a 4-byte selector and subsequent ABI-encoded data. The selector is computed in the same way as a function selector, i.e., the first four bytes of the keccak256-hash of a function signature - in this case an error signature.
-
Solidity supports two error signatures:
Error(string)
andPanic(uint256)
. The first (“error”) is used for “regular” error conditions while the second (“panic”) is used for errors that should not be present in bug-free code.
- State-reverting Exceptions
- Sub-call Exception -> Bubble-up
- Except ->
send
/*call
- External Calls ->
try
/catch
- Caller -> Selector+Data
Error(string)
-> RegularPanic(uint256)
-> Assertions