May 12, 2009 at 8:29am UTC
Hello,
I,m working with exception handling and do not understand some of the code in my assignment. What's the purpose of " : public logic_error " after the declaration ?
class TriangleException : public logic_error {
private:
double side1;
double side2;
double side3;
public:
TriangleException(double side1, double side2, double side3);
double getSide1();
double getSide2();
double getSide3();
};
May 12, 2009 at 9:46am UTC
TriangleException is inheriting from logic_error .
May 12, 2009 at 11:03am UTC
Thanks Grey Wolf
How does the class logic_error work? When I run my program I get the error message:
error C2512: 'std::logic_error' : no appropriate default constructor available
May 12, 2009 at 11:29am UTC
The logic_error constructor takes a const string reference in its constructor eg:
logic_error( "logic error" );
So you will need to call the base constructor appropriately, something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <iostream>
using namespace std;
class TriangleException : public logic_error {
private :
double side1;
double side2;
double side3;
public :
TriangleException(double side1, double side2, double side3);
double getSide1();
double getSide2();
double getSide3();
};
TriangleException::TriangleException(double side1, double side2, double side3)
:logic_error("TriangleException Logic Error" )
{
//constructor need finishing
}
//---------------------------------------------------------------------------
int main()
try
{
//throw logic_error( "logic error" );
throw TriangleException(1,1,1);
return 0;
}
catch ( exception &e )
{
cerr << "Caught: " << e.what( ) << endl;
cerr << "Type: " << typeid ( e ).name( ) << endl;
}
/* End of file ***************************************************************/
Last edited on May 12, 2009 at 11:31am UTC