bscodinglab

Error Handling

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1. The incorporation of the header file <errno.h> simplifies things for us.

So a C programmer can check the returned values and can take appropriate action depending on the return value. It is a good practice, to set errno to 0 at the time of initializing a program. A value of 0 indicates that there is no error in the program.

There are basically 2 types of functions associated with errno.

  • perror().
  • strerror()

The C programming language provides perror() and strerror() functions which can be used to display the text message associated with errno.

Perror():- The perror() function displays the string you pass to it, followed by a colon, a space, and then the textual representation of the current errno value.

Strerror():- The strerror() function, which returns a pointer to the textual representation of the current errno value.

Example Error Handling :-

#include <stdio.h>

#include <errno.h>

#include <string.h>

extern int errno ;

int main () {

   FILE * pf;

   int errnum;

   pf = fopen (“unexist.txt”, “rb”);

   if (pf == NULL) {

      errnum = errno;

      fprintf(stderr, “Value of errno: %d\n”, errno);

      perror(“Error printed by perror”);

      fprintf(stderr, “Error opening file: %s\n”, strerror( errnum ));

   } else {

      fclose (pf);

   }

   return 0;

}

Output :-      Value of errno: 2

Error printed by perror: No such file or directory

Error opening file: No such file or directory

Dividing by Zero Problem :-

It is the most popular problem discussed when dealing with error or exceptions.

We often give mathematical statements like:        c = a / b;

But we often tend to forget that in certain cases, the value of b is likely to be 0.

Mathematics defies the division of a number by 0. People often misconceive the answer to be infinite. Well, it is not the case. The answer, in this case, is “not defined”.

Here is a program that helps you deal with this problem by applying  the concepts of exception  or

error handling in C:

#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.

Lets check By An Example :-

#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