Constructor and destructor exceptions

Source: Internet
Author: User

Http://blog.csdn.net/jixingzhong/article/details/1844531

The constructor exception can be summarized as follows:
1. In C ++, the only way to notify the object construction failure is to throw an exception in the constructor;
2. When a constructor throws an exception, the Destructor will not be executed;
3. When an exception is thrown, its sub-objects are parsed in reverse order. (Refer to the analysis process)

Destructor exceptions are relatively complex, and a conflict exists,ProgramWill Crash directly: When an exception is called "Stack unwinding" [note], an exception is thrown from the destructor, c ++ runs in an uncertain situation. Therefore, the C ++ language guarantees that, at this point, the system will call terminate () to kill the process. Therefore, when processing another exception, do not throw an exception from the destructor. Summary:
1. The execution of destructor in C ++ should not throw an exception;
2. when some exceptions (even a little possible) occur in a destructor, you must completely encapsulate these exceptions in the destructor, never let it throw out the function (this is a perfect trick! Haha !);
3. When an exception is thrown, its sub-objects are parsed in reverse order. (Refer to the analysis process)

References:
[1] exception and error handling (C ++ FAQ), http://www.sunistudio.com/cppfaq/exceptions.html
[2] constructor thrown exception, http://51cmm.csai.cn/ExpertEyes/No143.htm
[3] exceptions thrown in destructor, http://se.csai.cn/ExpertEyes/No144.htm

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.