Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C Error handling


May 11, 2021 C


Table of contents


C Error handling

The C language does not provide direct support for error handling, but as a system programming language, it allows you to access the underlying data in the form of a return value. W hen an error occurs, most C or UNIX function calls return 1 or NULL, and an error code, errno, is set, which is a global variable that indicates that an error occurred during the function call. You can find a variety of error codes in the header file.

Therefore, the C programmer can decide which appropriate action to take by examining the return value and then deciding which one to take based on the return value. I t is a good programming practice for developers to set errno to 0 when the program is initialized. A value of 0 indicates that there are no errors in the program.

errno, perror() and strerror()

The C language provides perror() and strerror() functions to display text messages related to errno.

  • The perror() function displays the string you passed to it, followed by a colon, a space, and a text representation of the current errno value.
  • The strerror() function, which returns a text representation of the current errno value.

Let's simulate an error situation and try to open a file that doesn't exist. Y ou can output error messages in a number of ways, and here we use functions to demonstrate usage. It is also important to note that you should use the stderr file stream to output all errors.

#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;
}

When the above code is compiled and executed, it produces the following results:

Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory

Error divided by zero

When you do division, you do not check that the division is zero, which is a common problem for programmers when programming and can result in a run-time error.

To avoid this, the following code checks whether the division is zero before division:

#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);
}

When the above code is compiled and executed, it produces the following results:

Division by zero! Exiting...

The program exit state

Typically, a program successfully exits an operation with a value EXIT_SUCCESS. Here, EXIT_SUCCESS is a macro, which is defined as 0.

If there is an error condition in the program, when you exit the program, it comes with a status value EXIT_FAILURE, which is defined as -1. Therefore, the above program can be written as:

#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);
}

When the above code is compiled and executed, it produces the following results:

Value of quotient : 4