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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:43:24+00:00 2026-05-14T02:43:24+00:00

I have Java experience and recently am doing some C++ coding. My question is

  • 0

I have Java experience and recently am doing some C++ coding. My question is that if I have class A, in which I have to instantiate class B and class C as two of the member variables of A.

If in the constructor of A, should I assume that allocations of class B and C never fail, and handle the bad allocation exception in the destructor of A?

If I don’t make that assumption, meaning that I add some try catch block to catch bad_alloc of class B and class C, then if the allocation exception occurs, should I do clean up in the constructor of A?

What are the recommended practices? If “new” generates a bad allocation, what value does the pointer carry?

  • 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-14T02:43:24+00:00Added an answer on May 14, 2026 at 2:43 am

    If an exception is thrown during the construction of A, your destructor will not be called.

    Obviously the solution depends on what you’re doing, but ideally you won’t have to do any cleaning up. You should utilize RAII, and your class members should clean-up themselves.

    That is, don’t use any pointers raw; wrap them up and let the wrapper take care of it. Surprise! C++ programmers hate memory management just like you. We like to wrap it up and forget about it.

    If you truly need to, though, I think this is common:

    struct foo
    {
        int* i;
        some_member_that_could_throw crap;
    
        foo() // do *not* new i! if the second member throws, memory is leaked.
        {     // rather:
    
            // okay we made it, the other member must have initialized
            i = new int;
        }
    };
    

    Concerning your pointer, it’s value remains unchanged. When new throws an exception (for whatever reason), the stack is unwound. The rest of the expression is abandoned.


    Here’s how exceptions and object creation will work. It’s a recursive process, because each member or base class will in turn follow this list. Fundamental types have no constructors; this is the base case for the recursion.

    1. First, construct each of our base classes. (Which in turn run this list.)
    2. Initialize members of the class, one by one.
    3. Run the constructor body.
    4. finish with a fully constructed object.

    Obviously, if item 1 fails there isn’t any cleaning up for us to do, as none of our members have been initialized. We’re good there.

    Two is different. If at any point one of those fails to construct , the initialized members so far will be destructed, then the constructor will stop progress and the exception goes on it’s merry way. This is why when you let your members clean up after themselves you have nothing to worry about. The uninitialized have nothing to do, and the initialized are going to have their destructors run, where cleanup occurs.

    Three even more so. now that your objects are fully initialized, you’re guaranteed they will all have their destructors run. Again, wrap things up and you have nothing to worry about. However if you have a raw pointer lying around, this is the time for a try/catch block:

    try
    {
        // some code
    }
    catch (..) // catch whatever
    {
        delete myrawPointer; // stop the leak!
        throw; // and let the exception continue
    }
    

    It’s much messier to write exception-safe code without RAII.

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

Sidebar

Related Questions

Does anyone have any experience with doing this? I'm working on a Java decompiler
Let me first say that I have quite a lot of Java experience, but
I'm pretty new to Android development, but I have some experience with Java and
I have many years of experience in Java including Swing, Servlet and JDBC, but
Does Java have a built-in way to escape arbitrary text so that it can
Does Java have a data type that represents a period of time eg 34
I have a Java swing application with a panel that contains three JComboBoxe s
I have a java back-end that needs to expose services to clients running in
I have a lot of experience with java and c++ development, so classes and
I have a project for school and I have to use Java. Recently I

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.