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.