Advanced
Error Handling
BanglaCode provides a try-catch-finally mechanism using Bengali keywords:chesta (try), dhoro_bhul (catch), shesh (finally), and felo (throw).
Try-Catch (chesta-dhoro_bhul)
Use chesta (meaning "try" or "attempt") to wrap code that might throw errors, and dhoro_bhul (meaning "catch error") to handle them:
| 1 | chesta { |
| 2 | // Code that might fail |
| 3 | dhoro result = 10 / 0; |
| 4 | dekho(result); |
| 5 | } dhoro_bhul (error) { |
| 6 | // Handle the error |
| 7 | dekho("An error occurred:", error); |
| 8 | } |
| 9 | |
| 10 | // Output: An error occurred: division by zero |
Custom Error Types (v7.0.16)
BanglaCode provides JavaScript-compatible error types for more specific error handling:
| Error Type | Use Case | Example |
|---|---|---|
Error | Generic errors | General failures |
TypeError | Type mismatches | Expected number, got string |
ReferenceError | Undefined variables | Variable not defined |
RangeError | Out of range values | Index out of bounds |
SyntaxError | Syntax issues | Invalid JSON format |
Creating Custom Errors
| 1 | // Create typed errors |
| 2 | dhoro err1 = Error("Something went wrong"); |
| 3 | dhoro err2 = TypeError("Expected number, got string"); |
| 4 | dhoro err3 = ReferenceError("Variable 'x' is not defined"); |
| 5 | dhoro err4 = RangeError("Index out of bounds"); |
| 6 | dhoro err5 = SyntaxError("Invalid JSON"); |
| 7 | |
| 8 | // Throw custom errors |
| 9 | kaj validateInput(value) { |
| 10 | jodi (dhoron(value) != "NUMBER") { |
| 11 | felo TypeError("Input must be a number"); |
| 12 | } |
| 13 | jodi (value < 0 ba value > 100) { |
| 14 | felo RangeError("Value must be between 0 and 100"); |
| 15 | } |
| 16 | ferao sotti; |
| 17 | } |
Error Utility Functions
| Function | Description | Returns |
|---|---|---|
bhul_message(error) | Get error message | String |
bhul_naam(error) | Get error type name | String |
bhul_stack(error) | Get stack trace | String |
is_error(value) | Check if value is error | Boolean |
| 1 | // Using error utilities |
| 2 | chesta { |
| 3 | felo TypeError("Invalid type"); |
| 4 | } dhoro_bhul(e) { |
| 5 | dekho("Error name:", bhul_naam(e)); // "TypeError" |
| 6 | dekho("Error message:", bhul_message(e)); // "Invalid type" |
| 7 | dekho("Stack trace:", bhul_stack(e)); // Stack information |
| 8 | } |
| 9 | |
| 10 | // Check if value is an error |
| 11 | dhoro value = TypeError("test"); |
| 12 | jodi (is_error(value)) { |
| 13 | dekho("This is an error object"); |
| 14 | } |
Real-World Validation Example
| 1 | // User input validation with proper error types |
| 2 | kaj validateUser(user) { |
| 3 | // Type checking |
| 4 | jodi (dhoron(user) != "MAP") { |
| 5 | felo TypeError("User must be an object"); |
| 6 | } |
| 7 | |
| 8 | // Required fields |
| 9 | dhoro keys = chabi(user); |
| 10 | dhoro hasName = mittha; |
| 11 | dhoro hasAge = mittha; |
| 12 | |
| 13 | ghuriye (dhoro i = 0; i < kato(keys); i = i + 1) { |
| 14 | jodi (keys[i] == "name") { hasName = sotti; } |
| 15 | jodi (keys[i] == "age") { hasAge = sotti; } |
| 16 | } |
| 17 | |
| 18 | jodi (!hasName) { |
| 19 | felo ReferenceError("User must have 'name' property"); |
| 20 | } |
| 21 | jodi (!hasAge) { |
| 22 | felo ReferenceError("User must have 'age' property"); |
| 23 | } |
| 24 | |
| 25 | // Value validation |
| 26 | dhoro age = user["age"]; |
| 27 | jodi (dhoron(age) != "NUMBER") { |
| 28 | felo TypeError("Age must be a number"); |
| 29 | } |
| 30 | jodi (age < 0 ba age > 150) { |
| 31 | felo RangeError("Age must be between 0 and 150"); |
| 32 | } |
| 33 | |
| 34 | ferao sotti; |
| 35 | } |
| 36 | |
| 37 | // Usage with detailed error handling |
| 38 | dhoro user = {"name": "Rahim", "age": 25}; |
| 39 | |
| 40 | chesta { |
| 41 | validateUser(user); |
| 42 | dekho("User is valid!"); |
| 43 | } dhoro_bhul(e) { |
| 44 | dhoro errorType = bhul_naam(e); |
| 45 | dhoro errorMsg = bhul_message(e); |
| 46 | |
| 47 | dekho("Validation failed:"); |
| 48 | dekho(" Type:", errorType); |
| 49 | dekho(" Message:", errorMsg); |
| 50 | |
| 51 | // Handle different error types |
| 52 | jodi (errorType == "TypeError") { |
| 53 | dekho(" → Fix data types"); |
| 54 | } nahole jodi (errorType == "ReferenceError") { |
| 55 | dekho(" → Add missing fields"); |
| 56 | } nahole jodi (errorType == "RangeError") { |
| 57 | dekho(" → Check value ranges"); |
| 58 | } |
| 59 | } |
Finally Block (shesh)
The shesh block (meaning "end" or "finally") always executes, whether an error occurred or not. It's useful for cleanup:
| 1 | chesta { |
| 2 | dekho("Opening file..."); |
| 3 | // Risky operation |
| 4 | dhoro data = poro("nonexistent.txt"); |
| 5 | } dhoro_bhul (error) { |
| 6 | dekho("Error reading file:", error); |
| 7 | } shesh { |
| 8 | dekho("Cleanup: closing resources"); |
| 9 | } |
| 10 | |
| 11 | // Output: |
| 12 | // Opening file... |
| 13 | // Error reading file: file not found |
| 14 | // Cleanup: closing resources |
Finally Without Catch
| 1 | // You can use try-finally without catch |
| 2 | chesta { |
| 3 | dekho("Processing..."); |
| 4 | // Do something |
| 5 | } shesh { |
| 6 | dekho("Always runs"); |
| 7 | } |
Throwing Errors (felo)
Use felo (meaning "throw") to throw custom errors:
| 1 | kaj divide(a, b) { |
| 2 | jodi (b == 0) { |
| 3 | felo "Cannot divide by zero!"; |
| 4 | } |
| 5 | ferao a / b; |
| 6 | } |
| 7 | |
| 8 | chesta { |
| 9 | dhoro result = divide(10, 0); |
| 10 | dekho(result); |
| 11 | } dhoro_bhul (error) { |
| 12 | dekho("Error:", error); |
| 13 | } |
| 14 | |
| 15 | // Output: Error: Cannot divide by zero! |
Throwing Different Types
| 1 | // Throw string |
| 2 | felo "Something went wrong"; |
| 3 | |
| 4 | // Throw with details |
| 5 | felo "Invalid input: expected number"; |
| 6 | |
| 7 | // In practice, keep error messages descriptive |
| 8 | kaj validateAge(age) { |
| 9 | jodi (dhoron(age) != "int" ebong dhoron(age) != "float") { |
| 10 | felo "Age must be a number"; |
| 11 | } |
| 12 | jodi (age < 0) { |
| 13 | felo "Age cannot be negative"; |
| 14 | } |
| 15 | jodi (age > 150) { |
| 16 | felo "Age seems unrealistic"; |
| 17 | } |
| 18 | ferao sotti; |
| 19 | } |
Error Propagation
Errors propagate up the call stack until caught:
| 1 | kaj level3() { |
| 2 | dekho("Level 3: throwing error"); |
| 3 | felo "Error from level 3"; |
| 4 | } |
| 5 | |
| 6 | kaj level2() { |
| 7 | dekho("Level 2: calling level3"); |
| 8 | level3(); |
| 9 | dekho("Level 2: this won't execute"); |
| 10 | } |
| 11 | |
| 12 | kaj level1() { |
| 13 | dekho("Level 1: calling level2"); |
| 14 | level2(); |
| 15 | dekho("Level 1: this won't execute"); |
| 16 | } |
| 17 | |
| 18 | chesta { |
| 19 | level1(); |
| 20 | } dhoro_bhul (error) { |
| 21 | dekho("Caught error:", error); |
| 22 | } |
| 23 | |
| 24 | // Output: |
| 25 | // Level 1: calling level2 |
| 26 | // Level 2: calling level3 |
| 27 | // Level 3: throwing error |
| 28 | // Caught error: Error from level 3 |
Re-throwing Errors
| 1 | kaj processData(data) { |
| 2 | chesta { |
| 3 | // Process data |
| 4 | jodi (data == khali) { |
| 5 | felo "Data is null"; |
| 6 | } |
| 7 | // ... processing logic |
| 8 | } dhoro_bhul (error) { |
| 9 | dekho("Logging error:", error); |
| 10 | // Re-throw after logging |
| 11 | felo error; |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | chesta { |
| 16 | processData(khali); |
| 17 | } dhoro_bhul (error) { |
| 18 | dekho("Main handler caught:", error); |
| 19 | } |
Practical Patterns
Input Validation
| 1 | kaj validateUser(user) { |
| 2 | jodi (user == khali) { |
| 3 | felo "User object is required"; |
| 4 | } |
| 5 | jodi (user.naam == khali ba dorghyo(user.naam) < 2) { |
| 6 | felo "Name must be at least 2 characters"; |
| 7 | } |
| 8 | jodi (user.email == khali ba khojo(user.email, "@") < 0) { |
| 9 | felo "Valid email is required"; |
| 10 | } |
| 11 | jodi (user.boyosh == khali ba user.boyosh < 0) { |
| 12 | felo "Age must be a positive number"; |
| 13 | } |
| 14 | ferao sotti; |
| 15 | } |
| 16 | |
| 17 | chesta { |
| 18 | dhoro newUser = { |
| 19 | naam: "R", |
| 20 | email: "invalid-email", |
| 21 | boyosh: -5 |
| 22 | }; |
| 23 | validateUser(newUser); |
| 24 | dekho("User is valid"); |
| 25 | } dhoro_bhul (error) { |
| 26 | dekho("Validation failed:", error); |
| 27 | } |
Safe Division
| 1 | kaj safeDivide(a, b) { |
| 2 | chesta { |
| 3 | jodi (b == 0) { |
| 4 | felo "Division by zero"; |
| 5 | } |
| 6 | ferao { |
| 7 | success: sotti, |
| 8 | value: a / b, |
| 9 | error: khali |
| 10 | }; |
| 11 | } dhoro_bhul (error) { |
| 12 | ferao { |
| 13 | success: mittha, |
| 14 | value: khali, |
| 15 | error: error |
| 16 | }; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | dhoro result = safeDivide(10, 0); |
| 21 | |
| 22 | jodi (result.success) { |
| 23 | dekho("Result:", result.value); |
| 24 | } nahole { |
| 25 | dekho("Error:", result.error); |
| 26 | } |
API Response Handler
| 1 | kaj fetchData(url) { |
| 2 | chesta { |
| 3 | dhoro response = anun(url); |
| 4 | |
| 5 | jodi (response.status != 200) { |
| 6 | felo "HTTP Error: " + lipi(response.status); |
| 7 | } |
| 8 | |
| 9 | ferao response.body; |
| 10 | } dhoro_bhul (error) { |
| 11 | dekho("Failed to fetch data:", error); |
| 12 | ferao khali; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | kaj processApiData() { |
| 17 | dhoro data = fetchData("https://api.example.com/data"); |
| 18 | |
| 19 | jodi (data == khali) { |
| 20 | dekho("Using fallback data"); |
| 21 | data = {default: sotti}; |
| 22 | } |
| 23 | |
| 24 | // Process data... |
| 25 | ferao data; |
| 26 | } |
Retry Logic
| 1 | kaj retryOperation(operation, maxRetries) { |
| 2 | dhoro attempt = 0; |
| 3 | |
| 4 | jotokkhon (attempt < maxRetries) { |
| 5 | chesta { |
| 6 | ferao operation(); |
| 7 | } dhoro_bhul (error) { |
| 8 | attempt = attempt + 1; |
| 9 | dekho("Attempt", attempt, "failed:", error); |
| 10 | |
| 11 | jodi (attempt >= maxRetries) { |
| 12 | felo "Max retries exceeded. Last error: " + error; |
| 13 | } |
| 14 | |
| 15 | // Wait before retry |
| 16 | ghum(1000); // Wait 1 second |
| 17 | } |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | // Usage |
| 22 | chesta { |
| 23 | dhoro result = retryOperation(kaj() { |
| 24 | // Simulate unreliable operation |
| 25 | jodi (lotto() < 0.7) { |
| 26 | felo "Random failure"; |
| 27 | } |
| 28 | ferao "Success!"; |
| 29 | }, 5); |
| 30 | |
| 31 | dekho("Final result:", result); |
| 32 | } dhoro_bhul (error) { |
| 33 | dekho("Operation failed completely:", error); |
| 34 | } |
Resource Cleanup
| 1 | kaj processFile(filename) { |
| 2 | dhoro file = khali; |
| 3 | |
| 4 | chesta { |
| 5 | dekho("Opening file:", filename); |
| 6 | file = poro(filename); |
| 7 | |
| 8 | // Process file content |
| 9 | dekho("Processing", dorghyo(file), "characters"); |
| 10 | |
| 11 | // Simulate error |
| 12 | jodi (khojo(file, "ERROR") >= 0) { |
| 13 | felo "File contains error marker"; |
| 14 | } |
| 15 | |
| 16 | dekho("Processing complete"); |
| 17 | } dhoro_bhul (error) { |
| 18 | dekho("Error processing file:", error); |
| 19 | } shesh { |
| 20 | // Cleanup always runs |
| 21 | jodi (file != khali) { |
| 22 | dekho("Closing file resources"); |
| 23 | file = khali; |
| 24 | } |
| 25 | } |
| 26 | } |
Best Practices
- Be specific with error messages - Include relevant details about what went wrong
- Don't catch errors you can't handle - Let them propagate to where they can be properly handled
- Use finally for cleanup - Always release resources in the shesh block
- Log before re-throwing - Capture error context before propagating
- Validate early - Check inputs at function entry to fail fast
- Return result objects - For expected failures, consider returning success/error objects instead of throwing