Since C programming has no direct support for error handling, but being a system programming language, it provides access to lower levels based on the return value.
For any error, most C or even Unix function calls -1
or NULL
returns errno
set as the error code. It is set as a global variable and indicates that an error has occurred during the function call.
You may see different types of error codes that <errno.h>
are defined in the header file.
So a C programmer can check the return value and perform the appropriate action based on the return value.
errno
It is best to set its value to 0 (zero) during program initializing . A value of 0 indicates that there are no errors in the program.
C Programming Error Handling
C errno, perror() and strerror() functions
C programming language perror()
and strerror()
provides two functions with which errno
to display text messages associated with it.
-
perror()
errno
The function displays the textual representation for the string passed to it, followed by the colon(:) and its current value. -
strerror()
The functionerrno
returns a pointer to the textual representation of its current value.
Let’s try to open a non-existent file and throw an error. Here we have used both functions to demonstrate usage.
But you can use one or more methods of your choice to show the error. The second important point is that the file stream stderr
should be used to output all errors.
#include
#include
#include
extern int errno ;
int main () {
FILE * pf;
int errnum;
pf = fopen ("unexist.txt", "rb");
if (pf == NULL) {
errnum = errno;
fprintf(stderr, "Value of errno: %d\n", errno);
perror("Error printed by perror");
fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
}
else {
fclose (pf);
}
return 0;
}
When the above program is compiled and executed, the output will be as follows:
Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory
Error resulting from division by 0 (zero).
Oftentimes programmers do not check if the divisor is zero when dividing a number, which ultimately produces a runtime error.
The following program shows the solution to this problem by checking whether the divisor is 0 (zero) before dividing:
#include
#include
int main() {
int dividend = 20;
int divisor = 0;
int quotient;
if( divisor == 0){
fprintf(stderr, "Division by zero! Exiting...\n");
exit(-1);
}
quotient = dividend / divisor;
fprintf(stderr, "Value of quotient : %d\n", quotient );
exit(0);
}
When the above program is compiled and executed it will show the output as below:
Division by zero! Exiting...
Error resulting from division by 0 (zero).
After the program completes successfully, the program EXIT_SUCCESS
exits with its status value. Here EXIT_SUCCESS
is the macro and it is denoted by 0.
If there is an error in your program and you want to exit the program, EXIT_FAILURE
you must exit the program with a status that is denoted by -1.
So let’s write the above program as follows:
#include
#include
int main() {
int dividend = 20;
int divisor = 5;
int quotient;
if( divisor == 0) {
fprintf(stderr, "Division by zero! Exiting...\n");
exit(EXIT_FAILURE);
}
quotient = dividend / divisor;
fprintf(stderr, "Value of quotient : %d\n", quotient );
exit(EXIT_SUCCESS);
}
When the above program is compiled and executed, it will show the following output:
Value of quotient : 4