C + + constructors, destructors, and throw exceptions

Source: Internet
Author: User
Tags exception handling try catch
What does the throw () meaning of the constructor function:
At construction time, an exception may be thrown
When used, be careful to use try and catch

Explanation: Standard C + + defines a constructor as an object that constructs itself, allocates the required resources, and once the constructor has been executed, it indicates that the object has been born, has its own behavior and internal state of operation, and then the extinction process of the object (destructor execution). But who can guarantee that the object's construction process will be successful. Perhaps the system is not currently one of the resources, resulting in the object can not completely build their own, and because the C + + Standard Rules constructor is not return value, because the constructor does not return a value, so the only way to notify the construction failure of the object is to throw an exception in the constructor. Summary when throwing an exception in a constructor:
1 The only way to notify object construction failure in C + + is to throw an exception in the constructor; 2) throwing an exception in the constructor will cause the object's destructor not to be executed; 3 when the object is partially constructed, the constructed child object will be refactored in reverse order; 4 It is still the sentence, "C + + Exception handling does not break any of the object-oriented attributes. ”。

Additional resource Links http://www.cnblogs.com/hellogiser/p/constructor-destructor-exceptions.html the contents of the above links: constructors can throw exceptions. Destructors can be. Constructors can throw exceptions. Destructors can be. Analysis:
Syntactically, both constructors and destructors can throw exceptions. But logically and risk control, constructors can, destructors do not recommend throwing exceptions.

(1) The constructor can throw an exception: it is possible to throw an exception from the constructor whenever it is. There are two actions to create objects dynamically: allocating memory and calling constructors. If there is an error allocating memory, a Bad_alloc exception is thrown, and if there is an error in calling the constructor initialization, will there be a memory leak? The answer is no. The new operator guarantees that no memory leaks occur:

T *p = new T;

will be converted by the compiler to resemble the following:

    void Allocate_and_construct ()
    {
        //First step, allocate raw memory, if failure throws Bad_alloc exception
        try
        {
            //Step two, call constructor constructor object
            New (P) T;       Placement NEW: Only call constructor for T
        (...)
        {
            delete p;     Releases the memory throw that the first step allocates
            ;          Flip exception to notify Application
        }
    }

(2) Destructors do not recommend throwing exceptions, and if the destructor may throw an exception, then you must require that all exceptions be digested within the destructor or end the program. More effective C + + presents two reasons (destructors cannot throw exceptions) 1) If the destructor throws an exception, the program after the exception point does not execute, and if the destructor performs some necessary actions after the exception point, such as freeing some resources, the actions are not executed. Can cause problems such as resource leaks. [Normal invocation of a destructor throws an exception causing a resource leak] 2) usually occurs when the the C + + mechanism invokes the destructor of the constructed object to release the resource, and if the destructor itself throws an exception, the previous exception has not been processed and a new exception can cause the program to crash. [Invoking a destructor to throw an exception in the event of an exception can cause the program to crash] solution:
1 If an operation may throw an exception, class should provide a normal function (not a destructor) to perform the operation. The goal is to give the customer an opportunity to handle the error. 2 If an exception is thrown in a destructor, it is swallowed with a try catch, and the possible exception must be completely encapsulated inside the destructor, so it cannot be thrown out of the function.

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.