Solidity theorytheory 0/50 · 0%
Safety · easy

12. Errors: require, revert, custom errors

Failing loudly and cheaply.

A failed check reverts the whole transaction and undoes every state change, refunding the unused gas. `require` validates inputs and conditions; custom errors are the cheapest way to carry a reason.

error InsufficientBalance(uint256 available, uint256 needed);

function withdraw(uint256 amount) external {
    if (bal[msg.sender] < amount) revert InsufficientBalance(bal[msg.sender], amount);
    require(amount > 0, "zero");
}

`assert` is for invariants that should never fail — a failing assert signals a bug, not bad input.

Check your understanding

  1. 1. What happens to state changes when a call reverts?

  2. 2. Which is cheapest for a revert reason?

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