Composite object and exception Handling
Hi All,
i am new to C++.
in the given example , for given composite object if part of composite
object fails , compiler call destructor for
parts of composite objects for which constructor was called
successfully.
Since exception is the way for constructor to tell that they have
failed, but there we do not specify any unique Id to distinguish which
sub part of composite object has got failed.
Is it something specified in Language reference manual of C++ , how
compiler need to implement them.
++++++++++++++++++++++++++++++++++++++++++++++++++
#include <iostream>
using namespace std;
class B
{
public :
int * ptr;
B()
{
try
{ ptr = new int(4); }
catch(...)
{ delete []ptr; }
cout <<" Constructor of B Called "<< endl;
}
~B()
{
delete []ptr;
cout <<" Destructor of B Called "<< endl;
}
};
class C
{
public :
C() { cout <<" Constructor of C Called "<< endl; }
~C() { cout <<" Destructor of C Called "<< endl; }
};
class D
{
public :
// Here we intentionally throw Exception .
D() { throw 2; cout <<" Constructor of D Called "<< endl; }
~D() { cout <<" Destructor of D Called "<< endl; }
};
class E
{
public :
E() { cout <<" Constructor of E Called "<< endl; }
~E() { cout <<" Destructor of E Called "<< endl; }
};
class A : public B, public C
{
public :
D objD;
E objE;
A() : B(), C(), objD(), objE()
{ cout<<" contructor of A being Called "<< endl; }
~A()
{ cout<<" Destructor of A being Called "<<endl; }
};
int main()
{
try
{
A obj;
}
catch (...)
{
cout<<" exception caught for A being Caught "<< endl;
}
return 0;
}
Thanks
Pallav Singh