Reasons for the introduction of the exception in CPP and the slot for abnormal

Source: Internet
Author: User

See the discussion about the "CPP anomaly" in the know-how and sort it out here.


Reasons for introducing exceptions in C + +:

1, in order to allow the construction function error (destructors can not throw exceptions this is known to all common sense), after all, the constructor does not return a value, no exception if the caller how to know that the object construction is successful?

2, let dynamic_cast<derived&> (basereference) can error, because there is no null reference.

3, let overloaded operator can error, after all, operator return type often cannot contain error code, for example operator= () returned is type&. C + + is also the only language in which variable assignments can throw exceptions, such as person s; s = Getpersonbyid (Someid), then even if the Getpersonbyid () does not throw the exception can not guarantee that the last sentence assignment does not throw an exception. )

(In plain view, the opportunity to provide additional information to functions that do not provide additional information )
4, political reasons. ADA supports exceptions, and ADA is the official language of the DoD, and if C + + does not support exceptions, then T-Bell Labs will not be able to use C + + as DOD projects. )


The aspect of the exception mechanism of C + + is spit trough

The behavior of the entire C + + exception is the most exotic of all common languages, because this language feature is incompatible with other C + + feature (especially deterministic destructors). The full spread of usage exceptions in C + + encounters problems that do not exist in other languages. compiler/Standard library in order for the constructor to throw exceptions it is cumbersome:

    1. An array element is constructed with an exception, the elements that have been constructed before are refactored, and elements that are not yet constructed cannot be refactored.
    2. The constructor initializes the list of exceptions, and the members and base-class sub-objects that have been constructed before are refactored, and members that are not yet constructed cannot be refactored. And this exception must be re-thrown after it is caught (compiler coercion), because C + + does not allow objects constructed by "Dabbler" to exist.
    3. The constructor of a base class in multiple inheritance throws an exception, so the base class sub-object that has been constructed is to be deconstructed, and the base class sub-objects that have not been constructed cannot be refactored. Virtual inheritance, virtual base class can only be deconstructed once, you slowly think about it.
    4. When a function argument object constructs a throw exception, the actual parameter object that has been constructed in multiple arguments is to be deconstructed, and an argument object that has not yet been constructed cannot be refactored.
    5. Std::vector in the resizing when a copy of an element is abnormal, then the elements that have been copied before the destruction of the elements that have not yet been copied do not have to be able to refactor, to see the GCC vector::_m_insert_aux code is much trouble.

The C + + compiler is always wary of calling a function Foo throws an exception, which prevents optimizations and creates a lot of cumbersome code (ready to refactor objects that have been constructed before invoking the Foo function). Therefore, the noexcept of c++11 should be vigorously promoted.

Reasons for the introduction of the exception in CPP and the slot for abnormal

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.