Error Handling Best Practices
Guidelines for writing robust, maintainable error handling code.
Why this exists
Consistent error handling patterns make code more reliable and easier to maintain.
How it works
Follow principles like failing fast, providing context, and using appropriate error types. Prefer explicit error handling over silent failures.
Examples
# Fail fast with clear messages
fn connectDatabase(url) {
check url == null {
throw "Database URL is required"
}
check typeof(url) != "string" {
throw "Database URL must be a string"
}
# Attempt connection...
}
# Provide context in errors
fn loadUser(id) {
try {
return database.findUser(id)
} catch error {
throw "Failed to load user " + id + ": " + error
}
}
# Use finally for cleanup
fn processFile(path) {
set handle = null
try {
set handle = open(path)
return parseContent(handle.read())
} finally {
check handle != null {
handle.close()
}
}
}
Common mistakes
- Catching all errors generically
- Not cleaning up resources
- Providing vague error messages