Latest web development tutorials

C Error Handling

C language does not provide direct support for error handling, but as a systems programming language, it returns the value of the form allows you to access the underlying data. When an error occurs, most of the UNIX or C function call returns 1 or NULL, an error code will also seterrno, this error code is a global variable, it expressed during the function call error occurred.You can find a variety of error codes <error.h> header file.

Therefore, C programmers can return values ​​by checking what the appropriate action and then take decisions based on the return value. Developers should at program initialization, the errno set to 0, which is a good programming practice. A value of 0 indicates that the program no errors.

errno, perror () and strerror ()

C language providesperror () and strerror ()function to display a text message associated with theerrno.

  • perror () function to show your pass its string, followed by a colon, a space and the text representation of the current errno value.
  • strerror () function returns a pointer to the current value of errno text representation.

Let's simulate a false case, the attempt to open a file that does not exist. You can use several ways to output an error message, here we use the function to demonstrate the usage. Another thing to note, you should use thestderr output file stream to 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 produces the following results:

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

Division by zero error

During division, the divisor is zero if not checked, will result in a runtime error.

To prevent this from happening, the following code before making division will first check the divisor is zero:

#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 produces the following results:

除数为 0 退出运行...

Program exit status

Under normal circumstances, the successful implementation of the program after a normal operation time will exit with a value EXIT_SUCCESS. Here, EXIT_SUCCESS macro, which is defined as 0.

If there is a mistake in the program, when you quit the program, will carry status value EXIT_FAILURE, it is defined as -1. Therefore, the above procedure 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 produces the following results:

quotient 变量的值为 : 4