Basic Exception Handlingproblem

I have a piece of code
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
36
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
using namespace std;

class base
{
public:
	base(){ cout<<"base constructor"<<endl;};
	~base(){cout<<"base destructor"<<endl;};
	 void output()
	{
		cout<<"base exception"<<endl;
	}
};

class derived: public base
{
public:
	derived(){ cout<<"derived constructor"<<endl;};
	~derived(){cout<<"derived destructor"<<endl;};
	void output()

	{
		cout<<"derived exception"<<endl;
	}
};

int main()
{
	try{
		throw derived();
	}
	catch(derived ex)
	{
		ex.output();
	}
	catch(base ex)
	{
		ex.output();
	}
	
	catch(...)
	{
		cout<<"unknown exception thrown"<<endl;
	}

	return 0;
}

The output
calls the destructor of the 2 classes 3 times on VC++.
Why?
Because there are three objects ;)
Forget about that... I only get two objects... I thought you were surprised to see a destructor without a constructor, which would make sense since you don't implement your copy constructors. When I compile your code I see one object created (1 base & 1 derived ctor call) and two destroyed (2 base & 2 derived dtor calls). When I compile this here I see two objects created and two destroyed, as expected.

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
#include<iostream>
using namespace std;

class base
{
public:
    base(){ cout<<"base constructor of "<<this<<endl;}
    base(const base & b)
        {cout<<"base copy constructor of "<<this<<endl;}
    ~base(){cout<<"base destructor of "<<this<<endl;}

    void output(){cout<<"base exception"<<endl;}
};

class derived: public base
{
public:
    derived(){ cout<<"derived constructor of "<<this<<endl;}
    derived(const derived & b):base(b)
        {cout<<"derived copy constructor of "<<this<<endl;}
    ~derived(){cout<<"derived destructor of "<<this<<endl;}

    void output(){cout<<"derived exception"<<endl;}
};

int main()
{
    try {throw derived();}
    catch(derived ex){ex.output();}
    catch(base ex){ex.output();}
    catch(...){cout<<"unknown exception thrown"<<endl;}

    return 0;
}
Last edited on
Thanks for the reply ,
Putting the Copy contructors solves the problem, but my original posted code
On VC++ 2008 windows platform gives me 2 ctor( 1 base and 1 derived) and 6 dtor output statements. I don't have any idea about that.
Always catch exceptions by reference:
1
2
3
4
    try { /*...*/ }
    catch( derived & ex ) { /*...*/ }
    catch( base & ex ) { /*...*/ }
    //... 

Remember that constructors can throw!

Also, it appears that you're writing your own exception class very similar to std::exception. Consider always using std::exception as a virtual base for your exception classes. It is a very familiar base for other developers, library implementations, etc., and the virtual keyword prevents the future diamond problem if two of your exception classes are ever both inherited into a single subclass.
Last edited on
Topic archived. No new replies allowed.