I am trying to return error code from constructor, since constructor does not return an error code, I tried to put an exception on the constructor. Then in the catch block I return my appropriate error code. Is this a proper way of returning error code from constructor ?
#include <exception>
#include <iostream>
class A {
public:
A() { throw std::runtime_error("failed to construct"); }
};
int main() {
try {
A a;
} catch (const std::exception& e) {
std::cout << "returining error 1 \n";
return 1;
}
return 0;
}