C Error Handling

For handling of errors and exceptions, C language does not provide any direct support. However, there are many ways through which error handling can be done in C programming. These error codes are defined under the header file ‘error.h’ which will point out the error using return statement in a function. It is the task of the programmer to prevent errors in the first place and test return values from the function. A function will return -1 or a NULL value if errors are encountered and set the global variable errno. So the return value is an indication of error while programming. In the context of Socket [progrmming, the errors are determined by checking the returned value of functions like socket(), listen(), etc.

What is errno in C?

Whenever you call a function is called in C, a variable named errno is designated to it. The errno is a global variable, which can be used to identify the type of error encountered while executing the function based on its value. It will indicate an error occurred during any function call and defined in the header file ‘errno.h’.

Below you will see the list different error values along with its corresponding meaning.

errno valueError
1Operation not permitted
2No such file or directory
3No such process
4Interrupted system call
5I/O error
6No such device or address
7Argument list too long
8Exec format error
9Bad file number
10No child processes
11Try again
12Out of memory
13Permission denied

The C language will implement the functions like perror() and strerror() functions for printing the message associated with the errno.

  • perror(): The function perror() will return the string that is passed to it. It also returns the textual representation of the current errno value followed by a colon and space.
  •  strerror(): The function strerror() is defined under the library ‘string.h’. This method will return a pointer to the string which is the representation of the current value.

Example of error handling

Code:

#include <stdio.h>       
#include <errno.h>       
#include <string.h> 
 #include<stdlib.h>
int main ()
{
    FILE *fp;
    fp = fopen("AbsentFile.txt", "r");
    if (fp == NULL)
    {
    printf("Value of errno is %d\n ", errno);
    printf("The error message is  %s\n", strerror(errno));
    perror("The Message displayed from perror");
    exit(EXIT_FAILURE); 
        printf("I will not be printed\n"); 
    } 
  
 else
    { 
        fclose (fp); 
        exit(EXIT_SUCCESS); 
        printf("I wont be printed at this point \n"); 
    } 
    return 0;
}

The above example is a very simple example of a program implementing the concept of error handling. Here, when the program executes the program won’t find the file named ‘AbsentFile’. hence this file could not be opened. As this sort of error holds the value ‘2’. So the value of errno is ‘2’. And the function perror() will return the textual representation of the current errno value.

The above code will display its output as below.

Output:

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

What is divide by zero error?

A common mistake that a programmer makes during programming is not checking if the divisor is zero before executing the division command. In such a situation, nothing can be done to resolve the error. This error will lead to unwanted behavior and may even create a problem in the system. So in this case, the only thing we can do is to avoid the occurrence of such errors. The only possible way to avoid such error is by checking the denominator before using it in the program in the division operations. We can check for such errors by using the ‘if’ condition and if the denominator is found to be zero then display the message to alert the programmers about the possible error on the way and jump out of the function.

Now see the following code which illustrates the divide by zero error in C language and will help you understand more about this topic.

Code:

#include <stdio.h>
#include <stdlib.h>

main() {

   int numerator, denominator, quotient;
 printf("Enter the numerator: ");
 scanf("%d", &numerator);
  printf("Enter the denominator: ");
 scanf("%d", &denominator);
   if( denominator == 0){
      fprintf(stderr, "ALert! Divide error by zero is encountered in your program. \n");
      exit(-1);
   }
   
   quotient = numerator / denominator;
   fprintf(stderr, "Value of quotient is %d\n", quotient );

   exit(0);
}

The above code will display its output as below.

Output:

Enter the numerator: 5
Enter the denominator: 0
ALert! Divide by zero error is encountered in your program.

 

SHARE C Error Handling

You may also like...

Leave a Reply

Your email address will not be published.

Share