This Solidity smart contract demonstrates various error handling mechanisms and custom error types. It includes functions that use different error handling methods such as require
, revert
, and assert
, along with a custom error type defined within the contract.
To understand the error handling mechanisms and custom error type implemented in this contract, follow the instructions below:
-
Prerequisites: Ensure you have a compatible Ethereum development environment and compiler. This contract uses Solidity version 0.8.17.
-
Deploy the Contract: Deploy the
Error
contract to your preferred Ethereum development network (e.g., Ganache, Rinkeby, etc.). -
Interact with the Functions: Once the contract is deployed, you can interact with its functions to observe the different error handling methods.
This function uses the require
statement to validate if the input _i
is equal to 0. If the condition is not met, it reverts the transaction with the specified error message.
This function employs the revert
statement to check if the input _i
is less than or equal to 10. If the condition holds true, the transaction is reverted with the provided error message.
The testAssert
function uses the assert
statement to validate if the public state variable num
is equal to 0. If the condition is false, it triggers an assertion error.
This contract includes a custom error type named InsufficientBalance
. It accepts two parameters: balance
and withdrawAmount
. This custom error type can be used to revert transactions when a certain condition (insufficient balance in this case) is met.
The testCustomError
function uses the custom error InsufficientBalance
to check if the balance of the contract is less than the specified _withdrawAmount
. If the condition is satisfied, the transaction is reverted using the custom error with relevant data.
This contract is licensed under the MIT License. See the LICENSE file for details.
This project is created by Rajat Verma.
This contract is provided for educational purposes only. Use it in a controlled environment and do not use it in a production environment without proper security audits and testing.