Basic Error Functions
Cairo offers two main functions for error handling:1. assert
-
Used for condition validation (similar to Solidity’s
require) - Stops execution if the condition is false
-
Supports two formats:
2. panic
-
Used for immediate execution halt (similar to Solidity’s
revert) - Best for complex conditions or internal errors
-
Supports multiple formats:
Simple Example
Here’s a basic example demonstrating both error handling approaches:Custom Error Codes
For better organization and consistency, you can define error messages in a dedicated module:Real-World Example: Vault Contract
Here’s a practical example showing error handling in a vault contract that manages deposits and withdrawals:- Custom errors are defined in a separate module
- The
withdrawfunction demonstrates bothassertandpanicapproaches - Balance checks protect against underflow conditions