C Error Handling

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

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

errno, perror() and strerror()

C language providesperror()andstrerror()functions to displayerrnorelated text messages.

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

Let's simulate an error condition by trying to open a file that does not exist. You can use various ways to output error messages; here we use functions to demonstrate usage. Also, one thing to note is that you should usestderrfile streams to output all errors.

Example

#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, "Error number: %d\n", errno); perror("Output error via 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:

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

Division by zero errors

When performing division, if you do not check whether the divisor is zero, it will cause a runtime error.

To avoid this situation from occurring, the following code checks whether the divisor is zero before performing the division operation:

Example

#include <stdio.h> #include <stdlib.h> int 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, "The value of the quotient variable is : %d\n", quotient ); exit(0); }

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

除数为 0 退出运行...

Program exit status

In general, when a program successfully executes an operation and exits normally, it carries the value EXIT_SUCCESS. Here, EXIT_SUCCESS is a macro that is defined as 0.

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

Example

#include <stdio.h> #include <stdlib.h> int 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, "The value of the quotient variable is: %d\n", quotient ); exit(EXIT_SUCCESS); }

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

quotient 变量的值为 : 4
other extensions