Solidity theorytheory 0/50 · 0%
Errors · medium

22. Custom errors and require

Cheap reverts with structured data.

`require`, `revert` and `assert` all undo state changes. Custom errors encode a selector plus arguments instead of a string, so they are cheaper and machine-readable.

error InsufficientBalance(uint256 have, uint256 need);

if (bal < amt) revert InsufficientBalance(bal, amt);

Use `require` for input and state validation, custom errors for anything your UI must decode, and `assert` only for invariants that should be impossible to break.

Check your understanding

  1. 1. Why are custom errors cheaper?

  2. 2. What should `assert` be used for?

  3. 3. What happens to state on revert?