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 7622463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:27:39+00:00 2026-05-31T04:27:39+00:00

I have read several articles here and else where that it is OK to

  • 0

I have read several articles here and else where that it is OK to throw exception from constructor. However, I have noticed that it doesn’t call destructor of base class or its data members if an exception is thrown from the constructor. Consider the following example:

#include <iostream>
using namespace std;
struct C
{
    C() { cout << __FUNCTION__ << endl;  }
    ~C() { cout << __FUNCTION__ << endl; }
};

struct E: public C
{
    C c;
    E() { cout << __FUNCTION__ << endl; throw 4; }
    ~E() { cout << __FUNCTION__ << endl; }
};

int main()
{
    E e;
}


$ g++ test.cpp; ./a.exe
C
C
E
terminate called after throwing an instance of 'int'
Aborted (core dumped)

In this case, E’s constructor throws an exception but C’s destructor as a data member or as a base class is not called. Now if C’s destructor performs some cleanup operation like closing files/sockets and deleting heap allocations, this can cause problems.

So my question is why and when is it OK to throw exceptions from constructors.

  • 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-05-31T04:27:40+00:00Added an answer on May 31, 2026 at 4:27 am

    If you catch the error, the destructor will be run. When an uncaught exception is thrown in C++, the runtime calls std::terminate. By default, std::terminate calls std::abort which specifically does not call destructors on the way out.

    With this version:

    #include <iostream>
    using namespace std;
    struct C
    {
        C() { cout << __FUNCTION__ << endl;  }
        ~C() { cout << __FUNCTION__ << endl; }
    };
    
    struct E: public C
    {
        C c;
        E() { cout << __FUNCTION__ << endl; throw 4; }
        ~E() { cout << __FUNCTION__ << endl; }
    };
    
    int main()
    {
        try {
            E e;
        } catch(...) {
        }
    
        return 0;
    }
    

    I get output:

    C
    C
    E
    ~C
    ~C
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read several articles on when to use nested classes, but none that
I have read several documentations about building custom ErrorHandler (by inheriting from IErrorHandler). Unfortunately,
I have read in several places that it's possible to share the objects directory
I'm quite confused about several books in .NET that I have read. Would someone
Several articles and blogs I have read suggest placing assemblies I wish to make
I have read several articles/questions/forums discussing the best auto-complete plugin for jQuery. After trying
I have read several articles and several stackoverflow.com posts about expression tree. It is
I want to enable NSZombies for my iPhone app. I have read several articles
I've read many articles and several post (including here in stackoverflow) but do not
I've read several articles and posts that say that lock(this) , lock(typeof(MyType)) , lock(a

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.