Replace the exit () function with C + + exceptions

Source: Internet
Author: User
Tags exit printf

From C language to contact C + + people, I am afraid to know the exit () This function, it seems that many programmers now have such a habit, in the process of encountering errors, or the task just finished, call the exit () function as a best way to end the program. In many of the old-style C + + code that was left behind, this is a common phenomenon, but when the software project at hand is progressing and growing, you have to face the task of merging the previously dispersed modules, and if anyone remembers software logging, error tolerance, or at least proper cleanup, It's already a lucky one. The approach described in this article is by no means a design guideline, but relieves the pain of correcting old code that is not well designed and implemented.

Replacing exit with return is undoubtedly the most obvious way to solve this problem. If the software project is very simple, this is also the most efficient solution, however, the project often has a dozen functions distributed in multiple source files, and these function calls are nested in a very deep level, then things become tricky. If in this case, all functions return void, or it is possible to modify them to return an exit code (exit code), but the cost is great; if the function has been able to return a meaningful value, it is called exit () when an error is encountered. Then the job will become more time-consuming and more error-prone. This is said Han aside, using exit () is also desirable, when the old code does not design return anything, if you want to return code (returns code), only by exit ().

For this issue, there is a workaround, in which case, we assume that all the source code is in C + + format, or without all the compilation can be ported to C + + format, where all exit appears to replace all the throw (this can be done automatically, and even do not need to understand how the old code works) And then, in any appropriate place, catch the exception code for integers, which can also handle errors at different levels depending on severity or degree of recovery.

Take a look at the following example, the original code is as follows:

// main.cpp
void main() {
 //初始化
 ...
 ProcessMail(...);
}
//另一个源文件
void ProcessMail(...) {
 //初始化
 ...
 if ( initializationError ) {
  printf("faild to init!!!\n");
  exit(-1);
 }
 while ( !shutdown ) {
  ReadMail(...)
  //继续处理
  ...
 }
}
void ReadMail(...)
{
 ...
 //对ReadBytes()的调用出现在函数内的多处地方,包括在循环中。
 nBytesAvailable = ReadBytes(...)
 ...
}
//另一个源文件
int ReadBytes(...)
{
 //读取数据
 ...
 if ( error ) {
  printf("there was an error!!\n");
  exit(-1);
 }
 return nBytesRead;
}

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.