I've searched SO for an answer to this, but haven't found one.
When an object throws an exception at the end of the constructor, is the object valid or is this one of those 'depends on the construction technique'?
Example:
struct Fraction
{
int m_numerator;
int m_denominator;
Fraction (double value,
int denominator);
};
Fraction::Fraction(double value, int denominator)
: m_numerator(0), m_denominator(denominator)
{
if (denominator == 0)
{
/* E1 */ throw std::logic_error("Denominator is zero.");
}
m_numerator = static_cast<int>(value * static_cast<double>(denominator));
double actual_value = 0.0;
actual_value = static_cast<double>(m_numerator) / static_cast<double>(m_denominator);
double error = fabs(actual_value - value);
if (error > 5.0E-5)
{
/* E2 */ throw std::logic_error("Can't represent value in exact fraction with given denominator");
}
}
The program:
int main(void)
{
try
{
Fraction f1(3.14159264, 4); // Throws exception, E2 above.
}
catch (...)
{
cerr << "Fraction f1 not exactly representable as fraction with denom. of 4.\n";
}
// At this point, can I still use f1, knowing that it is an approximate fraction?
return EXIT_SUCCESS;
}
In this example, can f1 be used after the exception is caught, knowing that it is an approximate value?
The data members have been constructed and initialized.
I don't see any C++ language rule that is violated by the above.
Edit: Changed error delta value from 5.0E05 to 5.0E-5.