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

  • Home
  • SEARCH
  • 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 8303455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:32:52+00:00 2026-06-08T17:32:52+00:00

Constructor in my class checks for some condition. In some case it should break

  • 0

Constructor in my class checks for some condition. In some case it should break the creation of the object. Should I put there a destructor or just return statement?

It goes something like this:

Somewhere in the code:

new Obj( string );

and my constructor:

Obj::Obj( string ) {
    if( string == "something" ) {
        // should I put this here or only return?
        Obj::~Obj();
        return;
    }
    // ...
}

I know that I can check the condition before creation of the object, but I just wonder if it’s correct (if there are no memory leaks) because it compiles well without crashing at runtime.

  • 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-08T17:32:54+00:00Added an answer on June 8, 2026 at 5:32 pm

    Neither, you should throw an exception.

    No object will be created and this is the idiomatic way of dealing with this sort of situation.

    You’ll need to handle the exception in the calling context (or higher).

    Obj::Obj( string ) {
        if( string == "something" ) {
            // should I put this here or only return?
            throw ObjectCouldNotBeCreatedException();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across some interesting code in java.lang.Class#newInstance0: // Run constructor try { return
I perform some checks in my Window class constructor, and if they fail I
Into a class constructor, I need to create some objects on the fly and
I have a class constructor that expects a reference to another class object to
When you define a class constructor in a base class (i.e. to set some
If I have a javascript class which cannot be instantiated what should the constructor
I need to create an object of some type. The class of the object
I'm extending a constructor class' functionality via the prototype method, but I'm having trouble
I have a class constructor like this: public JavoImageCorrectedDataHeader() { ByteBuffer buffer = ByteBuffer.allocate(this.size());
I'm redesigning a class constructor in C++ and need it to catch an unspecified

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.