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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:53:51+00:00 2026-05-15T01:53:51+00:00

I have to create a set of wrapping C++ classes around an existing C

  • 0

I have to create a set of wrapping C++ classes around an existing C library.

For many objects of the C library, the construction is done by calling something like britney_spears* create_britney_spears() and the opposite function void free_britney_spears(britney_spears* brit).

If the allocation of a britney_spears fails, create_britney_spears() returns NULL.

This is, as far as I know, a very common pattern.

Now I want to wrap this inside a C++ class.

//britney_spears.hpp

class BritneySpears
{
  public:

    BritneySpears();

  private:

    boost::shared_ptr<britney_spears> m_britney_spears;
};

And here is the implementation:

// britney_spears.cpp

BritneySpears::BritneySpears() :
  m_britney_spears(create_britney_spears(), free_britney_spears)
{
  if (!m_britney_spears)
  {
    // Here I should throw something to abort the construction, but what ??!
  }
}

So the question is in the code sample: What should I throw to abort the constructor ?

I know I can throw almost anything, but I want to know what is usually done. I have no other information about why the allocation failed. Should I create my own exception class ? Is there a std exception for such cases ?

Many thanks.

  • 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-15T01:53:52+00:00Added an answer on May 15, 2026 at 1:53 am

    You would not want to derive a BritneyFailedToConstruct exception. My experience is that you should keep exception hierarchies as flat as possible (I use one single type per library). The exception should derive from std::exception, and should somehow contain a message that is accessible via std:;exceptions virtual what() function. You then throw it in your constructor:

    throw MyError( "failed to create spears object" );
    

    The following is the declaration for the exception class I use in my own utility library:

    class Exception : public std::exception {
    
        public:
    
            Exception( const std::string & msg = "" );
            Exception( const std::string & msg, int line,
                            const std::string & file );
    
            ~Exception() throw();
    
            const char *what() const throw();
            const std::string & Msg() const;
    
            int Line() const;
            const std::string & File() const;
    
        private:
    
            std::string mMsg, mFile;
            int mLine;
    };
    
    #define ATHROW( msg )\
    {   \
        std::ostringstream os;  \
        os << msg               \
        throw ALib::Exception( os.str(), __LINE__, __FILE__  ); \
    }   \
    

    The macro is for conveniently adding the file name and line number, and providing stream formatting for the message. This lets you say things like:

    ATHROW( "britney construction failed - bad booty value of " << booty );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rect object that I'd like to create and set its properties
I'm having trouble wrapping my head around how to set this up. I have
I have to create an app in which I must set a date and
I have been set a challenge to create an indexer that takes all words
I have set permission to allow anonymous user to create page. but when the
I have a set of transactions occurring at specific points in time: CREATE TABLE
I have been asked to create a vbscript that will set the default font
I have a set of JPEG images. I want to create wmv files from
Do you have some predefined set of targets which all build.xml files you create
I have tableview, where create new rows and set them layout manually. In every

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.