How do smart contracts handle errors?
Smart contracts handle errors by reverting the entire transaction, undoing every change as if it never happened. The three core tools are require, revert, and assert, each protecting users from different types of failure.
You send a transaction to a smart contract and something goes wrong. The transfer fails. Your gas fee is gone. You wonder if your money is stuck or lost forever. This is the moment where blockchain technology shows its true colors, because how a smart contract handles errors decides whether you get your funds back or watch them vanish.
Smart contracts do not crash like normal apps. They follow strict rules. When something breaks, the contract has only a few legal moves it can make. You need to know those moves before you sign another transaction.
Why smart contract errors hit you harder than regular bugs
A regular app crashes and you reload the page. A smart contract is different. Once you click confirm, the network records every step. There is no undo button. If the code does not handle the error well, your money can disappear into a contract address with no exit door.
Errors in blockchain technology happen for many reasons. The most common ones are:
- Not enough tokens in your wallet for the action
- A function called with the wrong input type
- A check inside the contract that fails on purpose
- The transaction running out of gas mid-execution
- An attacker trying to drain funds with a bad call
Each of these triggers a different response inside the contract. The good ones stop everything safely. The bad ones leave the contract in a broken state.
The three error tools every smart contract uses
Solidity, the main language for Ethereum smart contracts, gives developers three error-handling tools. Each one does a different job. You should recognize all three because they affect what happens to your money.
1. require
This is the front gate. The contract checks a condition before doing anything. If the condition fails, the whole transaction reverts. You get your remaining gas back. Your wallet stays untouched.
2. revert
This is a manual stop button. The developer triggers it inside the function when something is wrong. It works the same as require but lets the developer write a custom message. You see the message in your wallet and know why it failed.
3. assert
This is the panic button. It runs only when something should never happen, like a math overflow. If assert fails, the transaction reverts but the contract burns all your remaining gas. Developers use this for serious bugs, not user input mistakes.
A well-written smart contract uses require for user input, revert for business logic, and assert only for impossible situations. Mixing these up is one of the most common audit findings.
What revert really means for your wallet
When a contract reverts, the network erases every change made during that transaction. Token balances go back. Storage values reset. It is as if the call never happened. But two things still apply.
First, the gas you already spent on computation is gone. The miners or validators did the work and need to be paid. Second, the failed transaction still appears on the blockchain. Anyone can see it. Privacy is not part of the deal.
Solving the most painful errors with smart fixes
The hardest errors are the ones that do not revert. These leave your tokens trapped or send them to the wrong place. Three solutions have become standard in modern blockchain technology.
Try and catch. Solidity 0.6 added a try-catch system for external calls. If contract A calls contract B and B fails, A can catch the error and decide what to do. This stops one broken contract from breaking a whole chain of dApps.
Custom errors. Since Solidity 0.8.4, developers can define named errors with parameters. They are cheaper than long string messages. They also tell you exactly what went wrong, like InsufficientBalance with the missing amount included.
Checks-effects-interactions. This is a coding pattern, not a tool. The contract checks all conditions first, updates its own state next, and only then talks to other contracts. It blocks the famous reentrancy attack that drained 60 million dollars from The DAO in 2016.
How to protect yourself before signing
You cannot fix a smart contract from the outside. But you can avoid most painful errors with a short checklist.
- Read the contract on a block explorer. If the code is not verified, walk away.
- Check if the contract has been audited by a known firm. Audit reports are usually public.
- Test with a tiny amount first. Send 1 dollar worth of tokens before sending 1000.
- Use a wallet that simulates the transaction before you sign. Most modern wallets show you the expected outcome.
- Never approve unlimited token spending unless you trust the contract fully.
For deeper technical references on this topic, the official Ethereum documentation is the cleanest source. You can find it on the project's main site at ethereum.org.
The takeaway you need to remember
Smart contracts handle errors by reverting the whole transaction or by failing in ways that trap funds. Good contracts use require, revert, and assert correctly. Bad contracts skip checks and leak money. You stay safe by reading the code, testing small, and trusting only audited contracts. Blockchain technology is powerful but it does not forgive mistakes — yours or the developer's.
Frequently Asked Questions
- Do I get my gas back if a smart contract reverts?
- You get back the unused portion of your gas. The gas already spent on computation up to the point of failure is not refunded, because validators still did the work.
- What is the difference between require and assert in Solidity?
- require checks user inputs and conditions, refunding remaining gas on failure. assert is for impossible internal states and burns all remaining gas. Use require for normal validation, assert only for serious bugs.
- Can a failed smart contract transaction still cost me money?
- Yes. Even when a transaction reverts, you pay for the gas consumed before the failure. The token balance goes back to normal, but the gas fee is gone.
- What happens if a smart contract has no error handling?
- Without error handling, the contract may end up in a broken state where funds are stuck or stolen. This is why audits and patterns like checks-effects-interactions are essential.
- How can I tell if a smart contract is safe before using it?
- Check that the source code is verified on a block explorer, look for a public audit from a known firm, and test with a small amount first. Avoid contracts that ask for unlimited token approvals.