I am new to exception handling in C++, and recently I ran into a bit of an issue.
In my code I want to create an object and only one of them. I am interfacing with a library where I must provide inputs to the constructor. Here's what a call to the constructor would look like:
ObjectA my_object(param1, param2, param3);
My issue is that the constructor itself can throw exceptions. I have done limited work with exceptions in the past (I know about the try-catch mechanism), but I'm not sure what to do here due to variable scope. For example:
try {
ObjectA my_object(param1, param2, param3);
}
catch {
// don't worry I need to do more than this here, just an example...
cout << "OMFG!" << endl;
exit(EXIT_FAILURE);
}
// if code got here, everything with my_object is OK
my_object.Method1(param1); // ERROR: my_object is out of scope!
Some help would be appreciated in a quick way that I can check for the object being constructed correctly. Thanks