LAST UPDATED ON: SEPTEMBER 17, 2024
Error Handling in C
C language does not provide any direct support for error handling. However a few methods and variables defined in error.h header file can be used to point out error using the return statement in a function. In C language, a function returns -1
or NULL
value in case of any error and a global variable errno is set with the error code. So the return value can be used to check error while programming.
What is errno?
Whenever a function call is made in C language, a variable named errno
is associated with it. It is a global variable, which can be used to identify which type of error was encountered while function execution, based on its value. Below we have the list of Error numbers and what does they mean.
errno value | Error |
1 | Operation not permitted |
2 | No such file or directory |
3 | No such 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 |
C language uses the following functions to represent error messages associated with errno:
perror()
: returns the string passed to it along with the textual represention of the current errno value.
strerror()
is defined in string.h library. This method returns a pointer to the string representation of the current errno value.
Error Handling example in C
Let's see an example now,
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main ()
{
FILE *fp;
/*
If a file, which does not exists, is opened,
we will get an error
*/
fp = fopen("IWillReturnError.txt", "r");
printf("Value of errno: %d\n ", errno);
printf("The error message is : %s\n", strerror(errno));
perror("Message from perror");
return 0;
}
Value of errno: 2
The error message is: No such file or directory
Message from perror: No such file or directory
Other ways of Error Handling in C
We can also use Exit Status constants in the exit()
function to inform the calling function about the error. The two constant values available for use are EXIT_SUCCESS
and EXIT_FAILURE
. These are nothing but macros defined stdlib.h header file.
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
extern int errno;
void main()
{
char *ptr = malloc( 1000000000UL); //requesting to allocate 1gb 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
}
}
Here exit function is used to indicate exit status. Its always a good practice to exit a program with a exit status. EXIT_SUCCESS and EXIT_FAILURE are two macro used to show exit status. In case of program coming out after a successful operation EXIT_SUCCESS is used to show successful exit. It is defined as 0. EXIT_Failure is used in case of any failure in the program. It is defined as -1.
Division by Zero Example
There are some situation where nothing can be done to handle the error. In C language one such situation is division by zero. All you can do is avoid doing this, becasue if you do so, C language is not able to understand what happened, and gives a runtime error.
Best way to avoid this is, to check the value of the divisor before using it in the division operations. You can use if
condition, and if it is found to be zero, just display a message and return from the function.