Error Handling In C Language
For every c program it is required to have a proper error handling mechanism that can deal with errors which occur at runtime.Error handling is the process of responding to the occurrence of any error that occurs during the computation, of exceptions.In c programming language there is no support for error handling and garbage collection, the programmers have to make sure that a program comes out of an error condition gracefully. In C Programming Language error can be identified on the basis of return values of function calls, c function return -1 or NULL in case of any error condition and sets an error code for a global variable called errno which indicates an error occurred during any function call. You can find various error codes defined in header file. Based on these return values programmer can take proper action to handle the error.Programmer should set errno to zero (0) at the time of initialization of the program. A value of zero(0) indicates that there is no error in the program.

errno, perror() and strerror()

In C Programming Language we have perror() and strerror() functions which can be used to display the text message associated with errno.

perror() :  It displays the string you pass to it, followed by a colon, a space, and then the textual representation of the current errno value.
strerror() : It returns a pointer to the textual representation of the current errno value.
stderr : It is file stream to output the errors.
Example :
#include <stdio.h>        /* fprintf */
#include <errno.h>       /* errno */
#include <string.h>       /* strerror */

extern int errno ;

int main ()
{
   FILE * pf;
   errno = 0;
   pf = fopen ("notexist.txt", "r");
   if (errno || pf == NULL)
   {
      fprintf(stderr, "Value of errno: %d\n", errno);
      perror("Error");
      fprintf(stderr, "fopen failed due to: %s\n", strerror( errno));
   }
   else
   {
      fclose (pf);
   }
   return 0;
}
Output :

Value of errno : 2
Error : No such file or directory
fopen failed due to : No such file or directory

Divide by Zero Errors

It is a common problem that at the time of dividing any number, programmers do not check if a divisor is zero and finally it creates a runtime error.

Example Program : The code below fixes this by checking if the divisor is zero before dividing
#include <stdio.h>
#include <stdlib.h>

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);
}
Output :

Division by zero! Exiting...

Program Exit Status :

It is a common practice to exit with a value of EXIT_SUCCESS in case of program coming out after a successful operation. Here, EXIT_SUCCESS is a macro and it is defined as 0.

If you have an error condition in your program and you are coming out then you should exit with a status EXIT_FAILURE which is defined as -1.

Example Program : The code below fixes this by checking if the divisor is zero before dividing
#include <stdio.h>
#include <stdlib.h>

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);
}
Output :

Value of quotient : 4