Advanced Error Handling
Exception-Based Error Management
Custom Stream Exception Handling
#include <iostream>
#include <stdexcept>
#include <sstream>
class StreamException : public std::runtime_error {
public:
StreamException(const std::string& message)
: std::runtime_error(message) {}
};
void processInputStream(std::istream& input) {
try {
input.exceptions(std::ios::failbit | std::ios::badbit);
int value;
input >> value;
if (value < 0) {
throw StreamException("Negative value not allowed");
}
}
catch (const std::ios_base::failure& e) {
throw StreamException("Input stream failure");
}
}
Error Handling Strategy Workflow
graph TD
A[Input Received] --> B{Validate Input}
B -->|Valid| C[Process Data]
B -->|Invalid| D[Throw Custom Exception]
D --> E[Log Error]
E --> F[Recover/Retry]
Advanced Error Handling Techniques
Technique |
Description |
Implementation |
Exception Handling |
Throw custom exceptions |
Try-catch blocks |
Error Logging |
Record detailed error information |
Logging frameworks |
Graceful Degradation |
Provide fallback mechanisms |
Alternative processing |
Comprehensive Error Management
Multi-Level Error Handling
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <memory>
class InputHandler {
public:
enum class ErrorSeverity {
Low,
Medium,
High
};
class InputError : public std::runtime_error {
private:
ErrorSeverity severity;
public:
InputError(const std::string& message, ErrorSeverity sev)
: std::runtime_error(message), severity(sev) {}
ErrorSeverity getSeverity() const { return severity; }
};
static void processInput(std::istream& input) {
try {
int value;
if (!(input >> value)) {
throw InputError("Invalid input format",
ErrorSeverity::Medium);
}
if (value < 0) {
throw InputError("Negative value",
ErrorSeverity::High);
}
}
catch (const InputError& e) {
handleError(e);
}
}
private:
static void handleError(const InputError& error) {
switch (error.getSeverity()) {
case ErrorSeverity::Low:
std::cerr << "Warning: " << error.what() << std::endl;
break;
case ErrorSeverity::Medium:
std::cerr << "Error: " << error.what() << std::endl;
break;
case ErrorSeverity::High:
std::cerr << "Critical: " << error.what() << std::endl;
throw; // Rethrow for higher-level handling
}
}
};
Error Handling Patterns
stateDiagram-v2
[*] --> Normal : Initial State
Normal --> Error : Input Validation Fails
Error --> Logging : Record Error
Logging --> Recovery : Attempt Recovery
Recovery --> Normal : Retry Input
Recovery --> [*] : Terminate Process
Best Practices
- Use strongly typed exceptions
- Implement hierarchical error handling
- Provide detailed error context
- Enable flexible error recovery mechanisms
- Minimize exception overhead
- Use lightweight error handling mechanisms
- Implement efficient error logging
LabEx Insight
Explore advanced error handling techniques in the LabEx C++ programming environment to develop robust input processing strategies.
Error Categorization
enum class StreamErrorType {
FORMAT_ERROR,
RANGE_ERROR,
RESOURCE_ERROR,
PERMISSION_ERROR
};
struct ErrorContext {
StreamErrorType type;
std::string description;
int errorCode;
std::chrono::system_clock::time_point timestamp;
};