Market Cap: $3.3432T -2.41%
Volume(24h): $219.3876B 35.06%
Fear & Greed Index:

25 - Fear

  • Market Cap: $3.3432T -2.41%
  • Volume(24h): $219.3876B 35.06%
  • Fear & Greed Index:
  • Market Cap: $3.3432T -2.41%
Cryptos
Topics
Cryptospedia
News
CryptosTopics
Videos
Top Cryptospedia

Select Language

Select Language

Select Currency

Cryptos
Topics
Cryptospedia
News
CryptosTopics
Videos

How do you handle error-checking with require, assert, and revert?

Solidity's `require`, `revert`, and `assert` ensure secure smart contracts: use `require` for input validation, `revert` for custom errors, and `assert` for internal invariants.

Nov 14, 2025 at 05:19 pm

Understanding Error-Handling Mechanisms in Solidity

In the world of blockchain development, particularly within the Ethereum ecosystem, writing secure and reliable smart contracts is paramount. One of the core aspects of ensuring contract integrity involves proper error handling. Solidity provides several built-in functions to manage errors: require, revert, and assert. Each serves a distinct purpose and is used under specific circumstances to maintain logic correctness and prevent unintended behavior.

Using require for Input and Condition Validation

The require statement is primarily employed to validate inputs, external conditions, or state requirements before executing critical operations. It ensures that certain preconditions are met, such as sufficient balances, correct sender roles, or valid timestamps. If the condition inside a require statement evaluates to false, the transaction is reverted, and any changes made during execution are undone. Importantly, require refunds unused gas to the caller, making it efficient for validating user inputs.

  1. Use require to check function arguments like addresses or numerical values.
  2. Validate access control by confirming msg.sender has the appropriate role.
  3. Ensure time-based conditions are satisfied, such as checking block.timestamp against a deadline.
  4. Confirm token allowances or balances before transferring assets.
  5. Include descriptive strings in require statements to clarify the reason for failure.

Leveraging revert for Customized Error Handling

revert offers more granular control over error messages and conditions compared to require. While require automatically reverts when a condition fails, revert allows developers to trigger a revert at any point in the code with optional custom error messages. Since Solidity 0.8.4, developers can define custom error types using the error keyword, which saves gas by encoding errors more efficiently than string messages.

  1. Define custom errors using the error keyword to reduce gas costs on reverts.
  2. Call revert explicitly when complex logic determines an invalid state.
  3. Use structured error types like InvalidAddress() or InsufficientFunds(uint) for clarity.
  4. Trigger revert after detecting unexpected edge cases not covered by require.
  5. Combine revert with modifiers to centralize validation logic across multiple functions.

Applying assert for Internal Invariant Checks

assert is reserved for checking internal invariants—conditions that should never be false if the code is correctly implemented. It indicates a bug in the contract if triggered. Unlike require, assert consumes all remaining gas when it fails and should only be used to detect unrecoverable errors such as arithmetic overflows (prior to Solidity 0.8) or unexpected changes in stored data. With newer versions of Solidity, many arithmetic issues are handled automatically, reducing the need for manual asserts.

  1. Use assert to verify that a variable’s value remains within expected bounds after computation.
  2. Check that critical storage variables have not been corrupted during execution.
  3. Confirm unreachable code paths are indeed unreachable using assert(false).
  4. Avoid using assert for input validation or external conditions—it's meant for internal consistency.
  5. Understand that assert failures signal serious bugs requiring code fixes, not user corrections.

Frequently Asked Questions

What happens to gas when require fails?When a require statement fails, the transaction is reverted, and all state changes are rolled back. Unused gas is returned to the caller, minimizing cost for users who submit invalid inputs.

Can I use custom errors with require?No, require only accepts a boolean condition and an optional string message. To use custom error types defined with the error keyword, you must use revert instead.

Is assert still necessary in Solidity 0.8+?Its usage has diminished due to automatic overflow checks in Solidity 0.8 and above. However, it remains useful for verifying custom invariants or unexpected logical states that indicate a programming error.

Why choose revert over require?revert is chosen when you need to perform complex evaluations before deciding to abort execution or when leveraging custom errors for better readability and lower gas costs. It provides flexibility beyond the simple condition checking offered by require.

Disclaimer:info@kdj.com

The information provided is not trading advice. kdj.com does not assume any responsibility for any investments made based on the information provided in this article. Cryptocurrencies are highly volatile and it is highly recommended that you invest with caution after thorough research!

If you believe that the content used on this website infringes your copyright, please contact us immediately (info@kdj.com) and we will delete it promptly.

Related knowledge

What is a Denial of Service (DoS) attack in a smart contract and what are its common forms?

What is a Denial of Service (DoS) attack in a smart contract and what are its common forms?

Nov 10,2025 at 05:20am

Understanding Denial of Service in Smart Contracts1. A Denial of Service (DoS) attack in the context of smart contracts refers to a scenario where a m...

What is a cryptographic nonce used for in transaction signing?

What is a cryptographic nonce used for in transaction signing?

Nov 11,2025 at 05:59am

Understanding Cryptographic Nonces in Blockchain Transactions1. A cryptographic nonce is a random or pseudo-random number used only once in the contex...

How does inheritance work in Solidity smart contracts?

How does inheritance work in Solidity smart contracts?

Nov 11,2025 at 10:40pm

Inheritance in Solidity: Building Modular Smart Contracts1. Inheritance in Solidity allows one contract to adopt the properties and functions of anoth...

What is the difference between an Externally Owned Account (EOA) and a Contract Account?

What is the difference between an Externally Owned Account (EOA) and a Contract Account?

Nov 13,2025 at 04:00am

Understanding Externally Owned Accounts (EOA)1. An Externally Owned Account is controlled directly by a private key, which means only the holder of th...

What is the ERC-2981 NFT Royalty Standard and how does it work?

What is the ERC-2981 NFT Royalty Standard and how does it work?

Nov 13,2025 at 05:39am

Understanding the ERC-2981 NFT Royalty Standard1. The ERC-2981 standard is a proposed Ethereum Request for Comment that introduces a royalty mechanism...

What is a Minimal Proxy Contract (EIP-1167) and how does it save gas on deployment?

What is a Minimal Proxy Contract (EIP-1167) and how does it save gas on deployment?

Nov 12,2025 at 11:39am

What is a Minimal Proxy Contract (EIP-1167)?1. A Minimal Proxy Contract, standardized under Ethereum Improvement Proposal (EIP) 1167, is a lightweight...

What is a Denial of Service (DoS) attack in a smart contract and what are its common forms?

What is a Denial of Service (DoS) attack in a smart contract and what are its common forms?

Nov 10,2025 at 05:20am

Understanding Denial of Service in Smart Contracts1. A Denial of Service (DoS) attack in the context of smart contracts refers to a scenario where a m...

What is a cryptographic nonce used for in transaction signing?

What is a cryptographic nonce used for in transaction signing?

Nov 11,2025 at 05:59am

Understanding Cryptographic Nonces in Blockchain Transactions1. A cryptographic nonce is a random or pseudo-random number used only once in the contex...

How does inheritance work in Solidity smart contracts?

How does inheritance work in Solidity smart contracts?

Nov 11,2025 at 10:40pm

Inheritance in Solidity: Building Modular Smart Contracts1. Inheritance in Solidity allows one contract to adopt the properties and functions of anoth...

What is the difference between an Externally Owned Account (EOA) and a Contract Account?

What is the difference between an Externally Owned Account (EOA) and a Contract Account?

Nov 13,2025 at 04:00am

Understanding Externally Owned Accounts (EOA)1. An Externally Owned Account is controlled directly by a private key, which means only the holder of th...

What is the ERC-2981 NFT Royalty Standard and how does it work?

What is the ERC-2981 NFT Royalty Standard and how does it work?

Nov 13,2025 at 05:39am

Understanding the ERC-2981 NFT Royalty Standard1. The ERC-2981 standard is a proposed Ethereum Request for Comment that introduces a royalty mechanism...

What is a Minimal Proxy Contract (EIP-1167) and how does it save gas on deployment?

What is a Minimal Proxy Contract (EIP-1167) and how does it save gas on deployment?

Nov 12,2025 at 11:39am

What is a Minimal Proxy Contract (EIP-1167)?1. A Minimal Proxy Contract, standardized under Ethereum Improvement Proposal (EIP) 1167, is a lightweight...

See all articles

User not found or password invalid

Your input is correct