Error Handling in C
0 3347
There is no specific support for error handling in the 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 errors in programming.
Below is the list of error numbers and their meaning:
error value | error |
1 | no operation is 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 process |
11 | try again |
12 | out of memory |
13 | permission denied |
#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
Waiting for your comments