Error Handling
Understanding Error Handling Techniques in C Programming
Introduction to Error Handling (एरर हैंडलिंग का परिचय)
Error handling C programming में unexpected situations को manage करने का तरीका है। ये program को crash होने से बचाता है और user को meaningful error messages provide करता है।
Error handling is the way to manage unexpected situations in C programming. It prevents program crashes and provides meaningful error messages to users.
Key Points (मुख्य बिंदु):
- Error handling program reliability को improve करता है
- Error handling improves program reliability
- errno variable system errors को track करता है
- errno variable tracks system errors
- Function return values error checking के लिए use होते हैं
- Function return values are used for error checking
Error Detection
Error Handling
Error Recovery
errno Variable (errno वेरिएबल)
errno variable और error codes:
errno variable and error codes:
#include
#include
// Using errno
FILE *fp = fopen("nonexistent.txt", "r");
if (fp == NULL) {
printf("Error: %s\n", strerror(errno));
perror("fopen");
}
// Common error codes
if (errno == ENOENT) {
printf("File does not exist\n");
} else if (errno == EACCES) {
printf("Permission denied\n");
}
Error Code (एरर कोड) | Description (विवरण) |
---|---|
ENOENT | File या directory नहीं मिला |
ENOENT | File or directory not found |
EACCES | Permission denied |
EACCES | Permission denied |
Function Return Values (फंक्शन रिटर्न वैल्यूज)
Function return values और error checking:
Function return values and error checking:
// Memory allocation error checking
int *ptr = (int*)malloc(100 * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed\n");
exit(EXIT_FAILURE);
}
// File operation error checking
FILE *fp = fopen("data.txt", "w");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
// String operation error checking
char *str = strdup("Hello");
if (str == NULL) {
printf("String duplication failed\n");
return 1;
}
Common Return Values (कॉमन रिटर्न वैल्यूज):
- NULL pointer memory allocation errors के लिए
- NULL pointer for memory allocation errors
- -1 system call errors के लिए
- -1 for system call errors
- 0 successful operations के लिए
- 0 for successful operations
Custom Error Handling (कस्टम एरर हैंडलिंग)
Custom error handling techniques:
Custom error handling techniques:
// Error codes
#define ERROR_INVALID_INPUT 1
#define ERROR_FILE_NOT_FOUND 2
#define ERROR_MEMORY_ALLOC 3
// Error handling function
void handle_error(int error_code) {
switch(error_code) {
case ERROR_INVALID_INPUT:
printf("Invalid input provided\n");
break;
case ERROR_FILE_NOT_FOUND:
printf("File not found\n");
break;
case ERROR_MEMORY_ALLOC:
printf("Memory allocation failed\n");
break;
default:
printf("Unknown error occurred\n");
}
}
// Using custom error handling
int process_data(int value) {
if (value < 0) {
handle_error(ERROR_INVALID_INPUT);
return -1;
}
// Process data
return 0;
}
Best Practices (सर्वोत्तम प्रथाएं)
Error handling के best practices:
Best practices for error handling: