Interrupt during exception is handled in the output class using function-try-block approach

In the below code snippet, when a throwing exception from the base class constructor program was interrupted. I believe that the get class has already handled the exception as expected, but the program still exited and the program did not gracefully exit. Could you please let me know what is wrong with this approach, as if the exception were handled in the main function by imposing a try catch block on getting an object of class p, then the program does not break.

#include <iostream>
#include <memory>
using namespace std;
class A
{
  int *memory;
  public:
  A()
  {
   cout<<"calling constructor class A\n";
   memory= new int [100000000000000];

  }
  void show()
  {
  cout<<"show A\n";
  }
  ~A()
  {
    cout<<"calling destructor class A\n";
  }
};

class B : public A
{
  public:
 B()

  try :A() {
  cout<<"calling B constructor\n";
  }
  catch(...)
  {
  cout << "exception occurred\n";
  }
  ~B()
  {
    cout<<"calling B destructor\n";
  }


};

int main()
{
B obj;
obj.show();
return 1;
}

      

+3


source to share





All Articles