Error Handling Fundamentals
Effective error handling is crucial for creating robust and user-friendly applications. It involves detecting, reporting, and recovering from input-related issues.
Error Handling Strategies
Strategy |
Description |
Benefit |
Graceful Degradation |
Provide alternative actions |
Maintains user experience |
Clear Error Messaging |
Informative error descriptions |
Helps users understand issues |
Logging |
Record error details |
Assists in debugging |
Error Handling Workflow
flowchart TD
A[User Input] --> B{Validate Input}
B -->|Valid| C[Process Input]
B -->|Invalid| D[Detect Error Type]
D --> E[Generate Error Message]
E --> F{Retry Allowed?}
F -->|Yes| G[Prompt User Retry]
F -->|No| H[Terminate Process]
Comprehensive Error Handling Example
#define MAX_ATTEMPTS 3
typedef enum {
INPUT_SUCCESS,
INPUT_INVALID,
INPUT_OVERFLOW,
INPUT_UNDERFLOW
} InputStatus;
InputStatus handle_numeric_input(char *input, int *result) {
char *endptr;
int attempts = 0;
while (attempts < MAX_ATTEMPTS) {
errno = 0;
long value = strtol(input, &endptr, 10);
// Check for conversion errors
if (endptr == input) {
fprintf(stderr, "Error: No numeric input detected.\n");
attempts++;
continue;
}
// Check for overflow/underflow
if (errno == ERANGE) {
if (value == LONG_MAX) {
fprintf(stderr, "Error: Number too large.\n");
return INPUT_OVERFLOW;
}
if (value == LONG_MIN) {
fprintf(stderr, "Error: Number too small.\n");
return INPUT_UNDERFLOW;
}
}
// Validate input range
if (value < INT_MIN || value > INT_MAX) {
fprintf(stderr, "Error: Number out of integer range.\n");
return INPUT_INVALID;
}
*result = (int)value;
return INPUT_SUCCESS;
}
fprintf(stderr, "Maximum input attempts reached.\n");
return INPUT_INVALID;
}
int main() {
char input[100];
int result;
printf("Enter a number: ");
fgets(input, sizeof(input), stdin);
// Remove newline character
input[strcspn(input, "\n")] = 0;
InputStatus status = handle_numeric_input(input, &result);
switch (status) {
case INPUT_SUCCESS:
printf("Valid input: %d\n", result);
break;
case INPUT_INVALID:
printf("Invalid input processing.\n");
break;
case INPUT_OVERFLOW:
printf("Overflow error handling.\n");
break;
case INPUT_UNDERFLOW:
printf("Underflow error handling.\n");
break;
}
return 0;
}
Advanced Error Handling Techniques
- Use custom error types
- Implement comprehensive logging
- Provide meaningful error messages
- Create recovery mechanisms
Error Reporting Best Practices
- Be specific about error conditions
- Avoid exposing system internals
- Provide user-friendly guidance
- Log detailed error information
LabEx Insight
When developing error handling mechanisms, LabEx recommends creating modular error management functions that can be easily reused across different projects.
Error Mitigation Strategies
char* sanitize_input(char *input) {
// Remove non-numeric characters
char *sanitized = malloc(strlen(input) + 1);
int j = 0;
for (int i = 0; input[i]; i++) {
if (isdigit(input[i]) || input[i] == '-') {
sanitized[j++] = input[i];
}
}
sanitized[j] = '\0';
return sanitized;
}
2. Flexible Error Recovery
- Implement multiple error handling paths
- Provide user retry options
- Create fallback processing mechanisms