Skip to main content

Require / Revert / Assert

Explore Solidity error handling types — syntax, encoding, gas behavior, and usage guidance.

require()Input Validation Guard
1// Basic require
2require(msg.sender == owner);
3
4// With message
5require(amount > 0, "Amount must be positive");
6
7// Multi-condition
8require(balance >= amount, "Insufficient balance");
9require(block.timestamp >= unlockTime, "Too early");
10
11// Custom error (0.8.26+)
12require(amount > 0, InsufficientAmount());
require

Error(string) encoding:

08c379a0
0000...0020
0000...length
"Insufficient balance" (UTF-8 padded)
Error(string) selector
Offset to string data
String length in bytes
UTF-8 string data (padded to 32B)
Total: 132B
Properties
EVM OpcodeREVERT (0xfd)
SinceSolidity 0.4.22
Gas on FailureRefunds unused gas
Gas Overhead~200 gas overhead
Use CaseInput validation, access control, preconditions
BytecodeAdds REVERT opcode with Error(string) ABI encoding
Error DataABI-encoded Error(string) or none
Use require when:
  • Validating function inputs and parameters
  • Checking access control (msg.sender == owner)
  • Verifying preconditions before state changes
  • Checking external call return values
Don't use require for:
  • Internal invariant checks (use assert)
  • Complex error data (use custom errors)
  • Gas-sensitive operations (string messages cost extra)
What is require/revert?

require() validates conditions and reverts with a message. revert() explicitly aborts execution. Both use the REVERT opcode and refund remaining gas.

Assert vs Require

require is for expected failures (bad input), assert is for bugs (broken invariants). Since 0.8, both refund gas, but assert signals a code defect.

Custom Errors

Introduced in 0.8.4, custom errors are gas-efficient alternatives to string messages. They use only 4 bytes for the selector plus ABI-encoded parameters.

Error Encoding

Error data is ABI-encoded: Error(string) for require/revert messages, Panic(uint256) for assert, and custom selectors for user-defined errors.

Fun Fact

Before Solidity 0.8.0, assert() consumed ALL remaining gas — making it essentially a gas bomb if triggered accidentally.

Previous: Inheritance Visualizer
Back to inheritance visualizer