Exception Handling

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();
};
*bump*
closed account (z05DSL3A)
TriangleException is inheriting from logic_error.
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
closed account (z05DSL3A)
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
Topic archived. No new replies allowed.