Error handling in C
Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling the error in a lower level of the form of some return values. Many function calls either return -1 or NULL in case of any error.
This has been used to set the global variable, indicating an error that might occur during a function call. Error codes are defined within <error.h> header file. Hence, a developer can check the return value can act accordingly. It is advised to set errno to 0 at the time of initializing any program. If the value of errno returns a 0, then it can be concluded that there are no errors in that particular program or application.
Even though C generally never supports the handling of errors and exception, there are various ways to achieve First and foremost, a developer should avoid making errors, but we humans tend to make mistakes most of the time knowingly or unknowingly. Nevertheless, one can prevent errors by running test case values from the function.
Methods of handling errors in C:
- Global variable Errno
In the C standard, this variable is referred to as an ‘errno’ and is assigned with a specific number of codes that can be used within the program to detect the type of the error. Such a type of error is always declared in the file termed as an error.h, hence there are various errors such as;
errno value | error type |
1 | Operation not permitted |
2 | No such file or a directory |
3 | No process |
4 | Interrupted system call |
5 | I/O error |
6 | No such device or address |
7 | Argument list too long |
8 | Exec format error |
9 | Bad file number |
10 | No child processes |
11 | Try again |
12 | Out of memory |
13 | Permission denied |
E.g.:
#include <stdio.h> #include <errno.h> int main() { FILE *a; a = fopen(“Tutorials.txt” , “r”); printf (“The value of the error number as errno is: %d \n”, errno); return 0; }
Output
Since, we are trying to open and read a file that does not exist, it will hence give the error that is being assigned to a value, that is, errno 2.
prror() and strerror()
These are the two methods that are used to display the errno just the same way in the above program.
perror()
This function takes the message that has to be displayed which also shows the textual representation of the errno.
Syntax
void perror (const char *s)
Where,s is a string or message to be printed before the error message.
strerror()
This is the function that points to a string or a message of the errno value of this function. It is defined using <string.h> header library.
Syntax
char *strerror(int errno)
E.g.:
#include <stdio.h> #include <errno.h> #include <string.h> extern int errno ; int main () { FILE * f; int errornum; f = fopen ("article.txt", "rb"); if (f == NULL) { errornum = errno; fprintf(stderr, "The Value of errno: %d\n", errno); perror("Error message that is printed by perror"); fprintf(stderr, "Error message for opening file that does not exist: %s\n", strerror( errornum )); } else { fclose (f); } return 0; }
Output
Opening a file that is not present in the system so as to print the message using perror() and strerror() which again will print the message in accordance with the errno.
exit() status
The exit constant in this particular function is used to inform the calling function about the error. The constant values that will be ready for use are EXIT_SUCCESS and EXIT_FAILURE. There are macro defined libraries and are present within the <stdlib.g> header file.
E.g.:
#include <stdio.h> #include <errno.h> #include <stdlib.h> #include <string.h> extern int errno; void main() { char *ptr = malloc(100UL);/requesting to allocate memory space if (ptr == NULL) //if memory not available, it will return null { puts("malloc failed"); puts(strerror(errno)); exit(EXIT_FAILURE); //exit status failure } else { free( ptr); exit(EXIT_SUCCESS); //exit status Success } }
Divide by zero error
As the name itself defines, this error is displayed or will occur every time a compiler encounters a divisor zero before a division command. Hence, it leads to dividing by zero error.
E.g.:
#include<stdio.h> #include <stdlib.h> void function(int); int main() { int x = 0; function(x); return 0; } void function(int x) { float f; if (x==0) { printf("Division by Zero is not allowed as it leads to the error"); fprintf(stderr, "Division by zero error\n"); exit(EXIT_FAILURE); } else { f = 10 / x; printf("f(x) is: %.5f", f); } }
Output