Skip to main content

Exceptions in Destructors


When dog objects in main() go out of scope, it invokes destructor. Since destructor throws an exception, due to multiple exception condition program crashes.

class dog {
public:
string m_name;
dog(string name) 

m_name = name; 
cout << name << " is born." << endl; 
}

~dog() 

cout << m_name << " is distroied.\n" << endl;
throw 10;
}
};

int main()
{
try {
dog dog1("Henry");
dog dog2("Bob");
}
catch (int e) {
cout << e << " is caught" << endl;
}
return 1;
}



Solution 1: Destructor swallow the exception

~dog() { 
      try {
         // Enclose all the exception prone code here
      } catch (MYEXCEPTION e) {
         // Catch exception
      } catch (...) {
      }

   }



Solution 2:  Move the exception-prone code to a different function

class dog {
public:
         string m_name;
         dog(string name) {m_name = name; cout << name << " is born." << endl; }
 
                ~dog() { cout<< m_name << " is distroied.\n" << endl; }


                 void prepareToDestr() { throw 20; }
                 void bark() {cout<< m_name << " 's dog is barking.\n" << endl; }


};


int main{} {
  try {
     dog dog1("Henry");
     dog dog2("Bob");
     dog1.bark(); 
     dog2.bark();
     dog1.prepareToDestr();
     dog2.prepareToDestr();
  } catch (int e) {
  cout << e << " is caught" << endl;  // Only Exception of dog1 is caught
  }

}



Note: Destructor in main

class dog {
public:
         string m_name;
         dog(string name) {m_name = name; cout << name << " is born." << endl; }
 
                ~dog() { cout<< m_name << " is destroyed.\n" << endl; }


                
                 void bark() {cout<< m_name << " 's dog is barking.\n" << endl;  // It never gets invokes


};


int main{} {
  try {
     dog dog1("Henry");
     dog dog2("Bob");
   
      throw 20;

     dog1.bark(); 
     dog2.bark();
     
  } catch (int e) {
  cout << e << " is caught" << endl;  // Exception of dog1 is caught
  }

}


Output:

Henry is born.
Bob is born.
Bob is distroied.
Henry is distroied.


20 is caught

Comments

Popular posts from this blog

const used with functions

const used with functions : class Dog {    int age;    string name; public:    Dog() { age = 3; name = "dummy"; }         // const parameters and these are overloaded functions    void setAge(const int& a) { age = a; }    void setAge(int& a) { age = a; }         // Const return value    const string& getName() {return name;}         // const function and these are overloaded functions    void printDogName() const { cout << name << "const" << endl; }  // value of name can't be modified    void printDogName() { cout << getName() << " non-const" << endl; } }; int main() {    Dog d;    d.printDogName();        const Dog d2;    d2.printDogName();     }

Structured Bindings

Returning multiple Values from function C++ 11 vs C++ 17 Returning compound objects Iterating over a compound collection Direct initialization Returning multiple Values from function C++ 11 vs C++ 17 :  C++ 11 (std::tie): std::tuple mytuple() {     char a = 'a';     int i = 123;     bool b = true;     return std::make_tuple(a, i, b);  // packing variable into tuple } To access return value using C++ 11, we would need something like: char a; int i; bool b; std::tie(a, i, b) = mytuple();  // unpacking tuple into variables Where the variables have to be defined before use and the types known in advance. C++ 17 : auto [a, i, b] = mytuple(); Returning compound objects :  This is the easy way to assign the individual parts of a compound type (such as a struct, pair etc) to different variables all in one go – and have the correct types automatically assigned. So let’s have a look at an ...

Part1 : STL Algorithms (Non-Modifying sequence) in c++

Non-Modifying sequence operations : 1.  Non-modifying sequence operations:   _of (CPP 11) std::array all_of_elem = { 3,5,7,11,13,17,19,23 }; 1. if ( std::all_of ( all_of_elem.begin(),   all_of_elem.end() ,  [](int i) {return i % 2; } )) std::cout << "All the elements are odd numbers.\n"; std::array any_of_elem = { 0,1,-1,3,-3,5,-5 }; 2. if ( std::any_of ( any_of_elem.begin() ,  any_of_elem.end() ,  [](int i) {return i )) std::cout << "There are negative elements in the range.\n"; std::array foo = { 1,2,4,8,16,32,64,128 }; 3. if ( std::none_of ( foo.begin() ,  foo.end() ,  [](int i) {return i )) std::cout << "There are no negative elements in the range.\n"; 2.  Non-modifying sequence operations:   find       std::string myints[] = { "Hello", "Hi", "Bye", "ByeBye" }; std::vector myvector(myints, myints + 4); std::vector ::iterator it;...