Do not throw an exception from the destructor.

Source: Internet
Author: User

 C ++ programming specificationsArticle 51st: destructor, deallocation, and swap never fail.

 

I tried it today.

 

Today, I wrote a small program for my experiments. I checked every system call in the program. Once the call fails, a custom exception is thrown. At last, the main function captures all exceptions, then, output a friendly error message to the terminal.

 

After four or five hours of running the program, a dialog box is displayed, saying, "The xxx program has stopped working. Windows is checking the solution to this problem... "then asked me if I want to debug the program or cancel it. The terminal did not output any error messages that I prepared in advance.

 

Strange. This is generally caused by memory access exceptions, and my program does not have any array or pointer operations at all.

 

After a long time, I really clicked the "debug" button in the dialog box and opened Visual Studio to debug a lot of assembly code. Fortunately, although the program is built into the release version, you can still see the original C ++ code and runtime stack in Visual Studio after clicking "debug. Finally, we found that a system call failed while the program was running. My code failed to check and throw my custom exception. When the program throws an exception and performs stack unwinding, an object in the stack also throws an exception during the analysis. The class that the object has is defined by myself, and its destructor will also call a system function-of course, I also checked this system call, when an error occurs, the custom class is also thrown. Therefore, according to section 15.5.1 of the c ++ standard:

 

If a destructor called during stack unwinding exits with an exception,STD: TerminateIs called.

 

If an destructor throws an exception during stack rollback, the program will call STD: terminate.

 

What is the effect of calling STD: Terminate? As mentioned above.

 

Now you have to run the program for another few hours to know which system call went wrong.

 

This scene is a good explanation of what it means: "If you don't listen to the old man, you will suffer losses ."

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.