C error handling


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

So, C programmers can check the return value and then decide what appropriate action to take based on the return value. Developers should set errno to 0 during program initialization, which is a good programming practice. A value of 0 indicates that there are no errors in the program.

errno, perror() and strerror()

C language provides perror() and strerror() functions to display Text message related to errno.

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

  • strerror() Function, returns a pointer pointing to the text representation of the current errno value.

Let's simulate an error situation and try to open a file that doesn't exist. There are many ways you can output error messages, here we use functions to demonstrate usage. Another thing to note is 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, "错误号: %d\n", errno);
      perror("通过 perror 输出错误");
      fprintf(stderr, "打开文件错误: %s\n", strerror( errnum ));   }   else   {
      fclose (pf);   }   return 0;}

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

错误号: 2通过 perror 输出错误: No such file or directory打开文件错误: No such file or directory

Divide by zero error

When doing the division operation, if you do not check If the divisor is zero, a runtime error will result.

To prevent this from happening, the following code will check whether the divisor is zero before performing the division operation:

#include <stdio.h>#include <stdlib.h>main(){   int dividend = 20;   int divisor = 0;   int quotient; 
   if( divisor == 0){
      fprintf(stderr, "除数为 0 退出运行...\n");      exit(-1);   }
   quotient = dividend / divisor;
   fprintf(stderr, "quotient 变量的值为 : %d\n", quotient );   exit(0);}

When the above code is compiled and executed, it will produce the following Result:

除数为 0 退出运行...

Program exit status

Normally, when the program successfully executes an operation and exits normally, it will have the value EXIT_SUCCESS. Here, EXIT_SUCCESS is the macro and it is defined as 0.

If there is an error condition in the program, when you exit the program, it will be with the status value EXIT_FAILURE, which is defined as -1. So, 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, "除数为 0 退出运行...\n");      exit(EXIT_FAILURE);   }
   quotient = dividend / divisor;
   fprintf(stderr, "quotient 变量的值为: %d\n", quotient );   exit(EXIT_SUCCESS);}

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

quotient 变量的值为 : 4