Pretty simple question, how does one handle initializing a class member variable that possibly throws exceptions from it's constructor?
class main
{
public:
main() c(...) {};
private:
my_class c;
};
class my_class
{
public:
inline my_class() : o(...) { };
private:
some_obj o;
};
Clearly you can't try catch exceptions in a constructor initializer, so then would it be more appropriate instead to construct the object within a try catch block within the constructor?
This is a top-level class, so handling the exception to let a user know what happened and exiting gracefully is a priority over letting the program crash due to the exception?
class main
{
public:
main()
{
try
{
c(...);
}
catch(...)
{
...
}
};
private:
my_class c;
};
However, this wouldn't work, because the object gets initialized once before it does within the constructor, and therefore the program may crash if the object throws an exception.