| 146 | } |
| 147 | |
| 148 | void demonstrate_exception_handling() { |
| 149 | std::cout << "5. Exception Handling\n"; |
| 150 | std::cout << "=====================\n\n"; |
| 151 | |
| 152 | try { |
| 153 | // Most SDK functions use error return values instead of exceptions |
| 154 | // for better performance and clearer error handling |
| 155 | std::cout << "The AI SDK uses error return values instead of exceptions\n"; |
| 156 | std::cout << "for most operations. This provides:\n"; |
| 157 | std::cout << " - Better performance (no exception overhead)\n"; |
| 158 | std::cout << " - Clearer error handling code\n"; |
| 159 | std::cout << " - Easier debugging\n"; |
| 160 | std::cout << " - Predictable control flow\n\n"; |
| 161 | |
| 162 | // However, some operations may still throw for programming errors |
| 163 | std::cout << "Programming errors may still throw exceptions:\n"; |
| 164 | |
| 165 | // Example: trying to use moved-from objects, null pointers, etc. |
| 166 | // These are typically ai::ConfigurationError or std::logic_error |
| 167 | |
| 168 | } catch (const ai::AIError& e) { |
| 169 | std::cout << "AI SDK Error: " << e.what() << "\n"; |
| 170 | } catch (const std::exception& e) { |
| 171 | std::cout << "Standard exception: " << e.what() << "\n"; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void demonstrate_recovery_patterns() { |
| 176 | std::cout << "6. Error Recovery Patterns\n"; |