The error in this article refers to an error that occurs when the code compiles a completely correct program to run, because there is no successful invocation of some system call functions in the program. Often these system call functions use a return value (such as 1,0,-1) to indicate whether the call succeeds, and the programmer needs to know the detailed error information, so the self-built error capture function is necessary.
(1) errno and strerror ()
errno It is a cosmetic error code. When an error occurs, the system automatically assigns the error code to errno. Use the following method to get a specific description of the error:
- void My_err (Interror)
- {
- printf ("Error:%s with errno:%d\n", strerror (Error), error);
- Exit (1);
- }
- Intmain ()
- {
- ..............
- My_err (errno);
- ..............
- }
where char *strerror (int errnum) is passed errnum to get the error description, Errnum is the errno that is passed. Exit (1) at the end of the function causes the program to quit when an error occurs. However, the library function stdlib.h should be included.
Test the program below (source code at the end of this article). Use the open () function to create the file because the file being created already exists and the O_EXCL parameter is used, so open () generates an error. The results are as follows:
- [Email protected]:~/code$./error
- Error:file exists with errno:17
This method can display error messages and error codes in detail. However, you cannot display the number of rows where the error occurred.
(2) perror ()
Its function prototype is: void perror (const char *s). S is typically the function name. The function prints out the function name before printing the error message. The error message corresponds to the errno. The second parameter, __line__, is a macro that represents the current number of rows. How to use:
- void my_err2 (const char* Err_string,int line)
- {
- fprintf (stderr, "error:line:%d", line);
- Perror (err_string);
- Exit (1);
- }
- }
- int main ()
- {
- .................
- MY_ERR2 ("Open", __line__);
- ................
- }
The test results are as follows:
- [Email protected]:~/code$./error
- Error:line:29 Open:file exists
- }
the method can display the error message and the number of rows where the error occurred.
These methods are commonly used in "Linux C Programming", I made a small adjustment. Now combine the two methods:
- void My_err3 (Constchar*err_string,intline,interror)
- {
- printf ("error:line:%d%s ():%s with errno:%d\n", Line,err_string,strerror (Error), error);
- Exit (1);
- }
- Intmain ()
- {
- ................
- MY_ERR3 ("Open", __line__,errno);
- ................
- }
The test results are as follows:
- [email protected]: ~/code$./error
- Error:line:30 open (): File exists with errno:17
This allows you to display the error code, the error description, the number of rows in error, and the function where the error occurred. For beginners like me, it is important to note that the horizontal line before and after the macro __line__ is two consecutive underscores, not _line_, or there will be an error.
The source code is as follows:
Note: This program is only for testing purposes, in order to display the information of three error capture functions at the same time, the exit (1) of each function is masked. In addition, the article header file function with "" is because the display problem, there is no special significance.
Linux C system error