Error Handling in C

by Yogesh Khanna 28-Aug-18

0 493


There is no specific support for error handling in C program. However, there are some methods defined to find out the error using the return statement. In case of any error in C language, a function returns -1 or NULL value. A global variable errno is set for the error code. This can be used to check error in programming.

Below is the list of error numbers and their meaning
:

error valueerror
1no operation is permitted
2no such file or directory
3no such process
4interrupted system call
5I\O error
6no such device or address
7argument list to long
8exec format error
9bad file number
10no child process
11try again
12out of memory
13permission denied
Example

:

#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.text", "r");
printf("Value of errno: %d\n ", errno);
printf("The error message is : %s\n", sterror(errno));
perror("Message from perror");

return 0;
}

Result:

Value of errno: 2
The error message is: No such file or directory
Message from perror: No such file or directory


Share:

Comments

    No Comments Yet.

Leave a Reply