Error Handling
Error handling is essential to handle unexpected situations and ensure robust programs. C provides mechanisms like return values, error codes, and errno variable for error handling. Additionally, you can use perror and strerror functions to print error messages.
#include <stdio.h>
#include <errno.h>
int main() {
FILE* file = fopen("nonexistent_file.txt", "r");
if (file == NULL) {
printf("Error opening file: %s\n", strerror(errno));
perror("fopen");
return 1;
}
// File processing...
fclose(file);
return 0;
}
No comments:
Post a Comment