Composite object and exception Handling

P

Pallav singh

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
 
S

Saeed Amrollahi

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.

I quickly reviewed chapter 15 of N3290- Final Draft International
Standard (FDIS)
and I found nothing. In specific, section 15.2 doesn't mention any
particular details about what you want.
I believe the details of "Stack Unwinding" process implementation is
totally implementation-defined behavior.
BTW, I guess using a few boolean switches or log record you can
achieve the requirement. For example every fully constructed object
logs its construction.
++++++++++++++++++++++++++++++++++++++++++++++++++

#include <iostream>
using namespace std;

class B
{
  public :
   int * ptr;
   B()
   {
      try
      { ptr = new int(4); }
      catch(...)
      { delete []ptr; }

You allocated single integer, So you can release it using
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

HTH,
-- Saeed Amrollahi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top