"Effective C + +"-clause 08: Don't let exceptions escape destructors

Source: Internet
Author: User

Consider the following code:

class widget{public: ... ~widget () {...} // Suppose this might spit out an anomaly . }; void dosomething () {    std::vector<Widget>v;  } // V is destroyed here .

When Vector v is destroyed, it is responsible for destroying all the widgets it contains. Suppose V contains 10 widgets, and during the first element of the destructor, an exception is thrown. The other nine widgets should still be destroyed, so v should call each of their destructors. Suppose that during a call, the second widget destructor throws an exception. Now there are two simultaneous exceptions, which is too much for C + +. In the case where both exceptions exist, the procedure is not an end in execution, which results in ambiguous behavior. In this case, it leads to ambiguous behavior. A container or array is not a necessary condition for trouble, as long as the destructor highlights the exception, even if the container or arrays is not used, the program may end prematurely or appear ambiguous.

What if your destructor must perform an action that might throw an exception on failure?

"Effective C + +"-clause 08: Don't let exceptions escape destructors

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.