C-language error handling

Source: Internet
Author: User

Learn C + + when you do not pay much attention to this part of the knowledge, and later know that there is no basic C + + project to see the Try/catch. The Google programming specification even directly stated that I did not use the C + + feature, so I never understood the "simple" try/catch.

Looking at the C + + programming idea, I recorded some of the following. First, there are some error handling methods for C language.

1) The error message can be obtained by the return value of the function. If the function return value is not available, you can set a global error-judging flag (the errno () and perror () functions in the standard C language support this method). Because of the error checking for each function call, this is cumbersome and adds to the confusion of the program. Program designers may simply ignore these error messages, because tedious and confusing error checking must occur with each function call. In addition, the return value of a function from an accidental exception may not reflect any problems. This method of exception handling is very common in the Linux kernel, and can even be said to be standard practice. The result of the function is judged by the return value of the function, and a large number of goto statements are combined to implement error handling (such as the release of resources). More common is the situation of resource application, if the number of resources is more, there will be a complex program logic. A goto statement can easily make you a big head.

2) Use the signal () function (which determines the type of event occurrence) and the Raise () function (generating events) using a generally unfamiliar signal processing system in the C standard library. Since the user of the signal generation library must understand and install the appropriate signal processing system, it should be tightly integrated with each signal generation library, but for large projects, the signal between different libraries may conflict. The flaw of this method is more obvious, the user signal is limited, although can replace the processing function. But the conflict is almost inevitable, how do you know if there will be two errors at the same time, the result is a false signal is ignored.

3) Use the non-local jump function in the C standard library: setjmp () and longjmp (). The setjmp () function stores a typical normal state in the program and, if entered in an Error state, longjmp () restores the set state of the setjmp () function, and the location where the state is restored is closely linked to where the error occurred. Honestly, I seem to be the first to understand the effect of this thing, Khan! (But now I'm used to it, std::bind, and now I don't know!) )。 Here's an example: it's just a copy of the code on the next book.

#include <iostream> #include <setjmp.h>using namespace Std;class Rainbow {public:    Rainbow () {cout< < "Rainbow ()" <<endl;              }    ~rainbow () {        cout<< "~rainbow ()" <<endl;    }}; Jmp_buf kansas;void OZ () {    Rainbow RB;    for (int i=0;i<3;i++)    {        cout<< "There are no place like home\n";    }    LONGJMP (Kansas, n);    The value here is the return value of the setjmp when returning to the setjmp position. If set to 0, the return value is 1}int main () {    if (setjmp (Kansas) = = 0) {        cout<< "tornado, Witch, munchkins...\n";        OZ ();    } else {        cout<< "Auntie Em!"            << "I had the strangest dream...\n"            <<endl;    }}
As for the exception syntax of C + +, we all know it. But how is it implemented? This has always been a question. Then I saw Poplar's "C + + anomaly mechanism implementation and cost analysis" portal, do have a feeling of enlightened, recommend a bit.

C-language error handling

Related Article

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.