Error Handling¶
LeonBasic provides error handling capabilities using the try-catch mechanism, allowing you to gracefully handle exceptions that may occur during program execution.
1. try-catch Exception Handling¶
require("basic");
try {
var(a) = int:5 / int:0;
} catch(error) {
basic.print(string:"Error occurred: " + error);
}
2. Common Error Types¶
Here are some common errors you might encounter in LeonBasic:
- Division by zero errors
- File not found errors
- Type mismatch errors
- Network connection errors
- Module loading errors
3. Handling File Operation Errors¶
require("basic");
try {
var(file) = basic.open("nonexistent_file.txt", "read");
var(content) = basic.read(file);
basic.close(file);
} catch(error) {
basic.print(string:"File operation error: " + error);
}
4. Handling Network Errors¶
require("basic");
require("request");
try {
var(response) = request.get("https://unreachable-site.com");
basic.print(response);
} catch(error) {
basic.print(string:"Network error: " + error);
}
5. Handling Type Errors¶
require("basic");
try {
// Attempting to concatenate incompatible types
var(result) = string:"Hello" + int:123;
basic.print(var(result));
} catch(error) {
basic.print(string:"Type error: " + error);
}
6. Nested try-catch Blocks¶
You can nest try-catch blocks to handle errors at different levels:
require("basic");
try {
// Outer try block
var(file) = basic.open("data.txt", "read");
try {
// Inner try block
var(content) = basic.read(file);
var(invalid_operation) = int:10 / int:0;
} catch(inner_error) {
basic.print(string:"Error processing file content: " + inner_error);
}
basic.close(file);
} catch(outer_error) {
basic.print(string:"Error opening or closing file: " + outer_error);
}
7. Best Practices for Error Handling¶
- Always wrap operations that might fail (file operations, network requests, divisions) in try-catch blocks
- Provide meaningful error messages to help with debugging
- Ensure resources are properly released even when errors occur (e.g., close files)
- Consider implementing error recovery strategies where appropriate
- Log errors for troubleshooting purposes