Linux C language error handling

Source: Internet
Author: User

This article is from [http://www.cnblogs.com/feisky/archive/2009/11/17/1604686.html]

 

<Stdio. h> defines perror (). perror outputs the msg string on the standard output, and then adds the following error syntax (this error syntax corresponds to the errno at this time)

Strerror () is defined in <string. h>. strerror refers to an error numb as a parameter and returns the error CIDR Block corresponding to the error numb. errno is generally used.

# Include <stdio. h> # include <unistd. h> # include <sys/types. h> # include <string. h> # include <errno. h> int main (int argc, char * argv []) {int I = open ("/usr/src/s. t ", 0); perror (argv [0]); printf (strerror (errno); return 0 ;}execution result:] $. /test. /test: No such file or directory

<Assert. h> supports the assert () function. Note that when the assert () Check finds that the execution of the program is terminated by calling abort () when the conditions are not met, the application does not have the opportunity to clear the program. The role of assert is to calculate the expression. If its value is false (that is, 0), It prints an error message to stderr and then calls abort to terminate the program running.

The C standard defines the _ LINE _ and _ FILE _ macros. The gnu c extension defines the _ FUNCTION _ macro, which can be used for code tracking and debugging. These are three very useful global variables. When the program needs to output some content and wants to know where the output content is output, these global variables will come in handy. _ FILE __,__ FUNCTION __, _ LINE _ can be seen directly from the name, corresponding to: CodeFileName,FunctionName, row number.

<Errno. h> defines errno. Note that no function is used to clear errno. Therefore, errno is cleared before calling a function that may set errno. However, errno is a number, and the specific meaning must be read in errno. h. It is very tedious to check every time. Therefore, char * strerror (int errno) is often used to convert the error code into a string error message. You can combine this string and other information and output it to the user interface.

<Stdlib. h> defines the abort (), exit (), and atexit () functions.

The function abort () will cause the program to terminate abnormally. Before the termination, the program does not have the opportunity to execute the atexit () registration function, and does not plan to execute some regular cleanup tasks. At the same time, abort () also produces core dump, if there is no ulimit limit.

The exit () function is similar to abort (), but it terminates the program after cleaning.

Function atexit () registers the function to be called when the program ends normally.

<Syslog. h> defines syslogd interfaces, which are commonly used in system logs.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.