Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:44:19+00:00 2026-06-17T09:44:19+00:00

I used to think that in C++, if a constructor throws an exception, the

  • 0

I used to think that in C++, if a constructor throws an exception, the destructor of this “partially constructed” class is not called.

But it seems that it is not true anymore in C++11: I compiled the following code with g++ and it prints “X destructor” to the console. Why is this?

#include <exception>
#include <iostream>
#include <stdexcept>
using namespace std;

class X
{
public:
    X() : X(10)
    {
        throw runtime_error("Exception thrown in X::X()");    
    }
    X(int a)
    {
        cout << "X::X(" << a << ")" << endl;
    }
    ~X()
    {
        cout << "X destructor" << endl;
    }
};

int main()
{
    try
    {
        X x;
    }
    catch(const exception& e)
    {
        cerr << "*** ERROR: " << e.what() << endl;
    }
}

Output

Standard out:
X::X(10) 
X destructor
Standard error: 
*** ERROR: Exception thrown in X::X()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:44:19+00:00Added an answer on June 17, 2026 at 9:44 am

    Delegating constuctors are indeed a new feature that introduces a new destruction logic.

    Let us revisit the lifetime of an object: An object’s lifetime begins when some constructor has finished. (See 15.2/2. The standard calls this the “principal constructor”.) In your case, this is the constructor X(int). The second, delegating constructor X() acts as just a plain member function now. Upon scope unwinding, the destructors of all fully-constructed objects are called, and this includes x.

    The implications of this are actually quite profound: You can now put “complex” work loads into a constructor and take full advantage of the usual exception propagation, as long as you make your constructor delegate to another constructor. Such a design can obviate the need for various “init”-functions that used to be popular whenever it wasn’t desired to put too much work into a regular constructor.

    The specific language that defines the behaviour you’re seeing is:

    [C++11: 15.2/2]: [..] Similarly, if the non-delegating constructor for an object
    has completed execution and a delegating constructor for that object exits with an exception, the object’s destructor will be invoked. [..]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think that I've been used to a fairly liberal policy regarding the PHP
A recent question contains a problem that I many times used to think about
I think I may misunderstand why the with command is used. But can any
Pardon me for this long story, but I think the question merits it. I
I'm not sure exactly how to describe this question, but here goes. I've got
I have a C++ unmanaged class NativeDog that needs to be used from C#,
I have a simple class ExcelStringBuilder. Its used to build a string that can
Note: My code happens to be in C#, but I don't think that's significant
Before answering, please note that this is a client side issue and not a
I used to think that both assignments a = foo and a = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.