Error management for large-scale projects in C + +

Source: Internet
Author: User

Error management plays an important role in the project, with my own project experience and other projects, and error management has a great impact on the project framework and development efficiency. The understanding of error management is broadly divided into three categories:

    1. New to the beginning of the code, the full program can not see a deal about the return error, let alone error management. He didn't realize the importance of error management.
    2. The processing of errors can be seen everywhere in the program. Aware of mistakes, but lack of treatment
    3. It's almost not obvious in the program to see the handling of the error. This is the highest error management situation.

Error management involves the robustness, recoverability, reliability, and efficiency of the program. In the event of an error, the program can run relatively robustly and efficiently.

I'll talk about my own experience with mis-management in C + + project development below.

The errno.h header file is provided for the C + + system API, which has already defined the majority of system-known errors and their corresponding error prompts. "Error handling in Linux"

The general system comes with the error definition and error hints are not very good to meet the actual project needs, after all, the project is very much involved in the detailed business, assuming that the general use of system-defined error numbers and descriptive narrative, and can not very quickly define the wrong location. We usually need to encapsulate and define the error ourselves again.


One principle in the Art of Unix programming is that it is very good: rule of repair:when You must fail, fail noisily and as soon as possible. When the program encounters an error that cannot be repaired, let it immediately make an error in a very obvious way. This sentence is very concise, but the lack of descriptive description of the concept of mis-management. The assumption is that it must be an error, in a very obvious way, in order to pinpoint errors very quickly and precisely. Error is not expected, error management is not an end, the purpose is to be able to quickly locate errors after the error so that the program can be very fast recovery.


For high-speed positioning errors, there are three of macros that are useful:

__file__             //File name __function__   //function name __line__            //Current line number

Generally for different business will have different modules. The way you define the error number is as follows:

Ebase the highest bit is 1, to ensure that the error number is negative, in line with the programming habit #defineebase0x80000000//Serial Business module #defineeserialebase + 0xc8#defineeserialtimeout  Eserial + 1//serial port timeout #defineeserialreadeserial + 2//Read serial error//....//http business module, and Eserial there is a gap between 200, so the serial business module can have 200 error number # Defineehttpserviceebase + 0x190#defineehttptimeehttpservice + 1#defineehttpgetehttpservice + 2//...// There are 200 gaps with ehttpservice, so HTTP can also have 200 error numbers #defineegsmebase + 258//....

Modular definition error, the error number interval can be very fast access to the error module. For example, the error number is-202, by contrast, there is no doubt that the serial module error.


The next is the wrong translation. The so-called error translation is the translation of the error number into detailed statements. can choose to write the error description and error number into the Excel table, and then export the XML file, in the program error description can directly parse the corresponding XML error number of descriptive narrative can be.

Another common way is to write directly in a header file with a macro. Of course, __file__, __function__ these are indispensable.


The log file records the status of the program's background and what the program is doing the moment before the error, generally able to analyze the program's behavior and locate the error from the log file's data. For the log file, I would like to write a C/C + + program Ape, I don't say much here. The usual print error prompts are accompanied by a write log at the same time.

General will use macros to write print errors and write the operation of the log, about how to write I don't say much here, I think very many people will. Here's what I want to stress about printing error messages and writing logs be sure to set the switch. In the actual project, I often encounter this problem, when there are very many places need to print and record log, the program is very slow to execute, when I remove the Write log, the program can be executed very quickly. Therefore, it is necessary to set the switch:

#ifndef log_off#define writelog (file, line, function, desc)  //... Some write logs and print operations #else#define  

This allows for very good control of the switch in the program. Increase efficiency



Error management for large-scale projects in C + +

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.