title: When do destructors get called when multiple exceptions handlings are involved?
body: <p>In the following code, the exceptions are thrown in two cases as shown in the main ().</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  // Our own exception classes - just for fun.
  class myExceptionClassA 
  {
      public:
      myExceptionClassA () {std::cout &lt;&lt; "\nWarning: Division by zero isn't allowed.\n";}
  };
  
  class myExceptionClassB
  {
      public:
      myExceptionClassB () {std::cout &lt;&lt; "\nWarning: Division by dividend isn't allowed.\n";}
  };
  
  class divisionClass
  {
      private:
      int *result;
  
      public:
      divisionClass () 
      {
          // Allocating memory to the private variable.
          result = new int;
      }
  
      /* 
          The class function `doDivide`:
          1. Throws above defined exceptions on the specified cases.
          2. Returns the division result.
      */
      int doDivide (int toBeDividedBy) throw (myExceptionClassA, myExceptionClassB)
      {
          *result       = 200000;
  
          // If the divisor is 0, then throw an exception.
          if (toBeDividedBy == 0)
          {
              throw myExceptionClassA ();
          }
          // If the divisor is same as dividend, then throw an exception.
          else if (toBeDividedBy == *result)
          {
              throw myExceptionClassB ();
          }
  
          // The following code won't get executed if/when an exception is thrown.
          std :: cout &lt;&lt;"\nException wasn't thrown. :)";
  
          *result = *result / toBeDividedBy;
          return *result;
      }
  
      ~divisionClass () 
      {
          std::cout &lt;&lt; "\ndddddddddd\n";
          delete result;
      }
  };
  
  int main ()
  {
      divisionClass obj;
      try
      {
          obj.doDivide (200000);
      }
      catch (myExceptionClassA) {}
      catch (myExceptionClassB) {}
  
      try
      {
          obj.doDivide (3);
      }
      catch (myExceptionClassA) {}
      catch (myExceptionClassB) {}
  
      try
      {
          obj.doDivide (0);
      }
      catch (myExceptionClassA) {}
      catch (myExceptionClassB) {}
  
      try
      {
          obj.doDivide (4);
      }
      catch (myExceptionClassA) {}
      catch (myExceptionClassB) {}
  
      return 0;
  }
  </code></pre>
  
  <p><strong>- The both exception class print statements get printed.<br>
  - The statement in the destructor gets printed only once.<br>
  - Valgrind doesn't show any memory leaks.</strong></p>
  
  <pre><code>anisha@linux-y3pi:~/Desktop&gt; g++ exceptionSafe3.cpp -Wall
  anisha@linux-y3pi:~/Desktop&gt; valgrind ./a.out 
  ==18838== Memcheck, a memory error detector
  ==18838== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
  ==18838== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
  ==18838== Command: ./a.out
  ==18838== 
  
  Warning: Division by dividend isn't allowed.
  
  Exception wasn't thrown. :)
  Warning: Division by zero isn't allowed.
  
  Exception wasn't thrown. :)
  dddddddddd
  ==18838== 
  ==18838== HEAP SUMMARY:
  ==18838==     in use at exit: 0 bytes in 0 blocks
  ==18838==   total heap usage: 3 allocs, 3 frees, 262 bytes allocated
  ==18838== 
  ==18838== All heap blocks were freed -- no leaks are possible
  ==18838== 
  ==18838== For counts of detected and suppressed errors, rerun with: -v
  ==18838== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
  anisha@linux-y3pi:~/Desktop&gt; 
  </code></pre>
  
  <p><strong>Shouldn't the destructor get called 3 times - two times for exceptions and one time for return statement?</strong></p>
  
  <p>Please explain the point that I am missing.</p>
  
  <hr>
  
  <p>Now I tried it by removing all the try catch blocks in the main().<br>
  Destructor doesn't get called at all?</p>
  
  <pre><code>anisha@linux-y3pi:~/Desktop&gt; valgrind ./a.out 
  ==18994== Memcheck, a memory error detector
  ==18994== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
  ==18994== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
  ==18994== Command: ./a.out
  ==18994== 
  
  Warning: Division by dividend isn't allowed.
  terminate called after throwing an instance of 'myExceptionClassB'
  ==18994== 
  ==18994== HEAP SUMMARY:
  ==18994==     in use at exit: 133 bytes in 2 blocks
  ==18994==   total heap usage: 3 allocs, 1 frees, 165 bytes allocated
  ==18994== 
  ==18994== LEAK SUMMARY:
  ==18994==    definitely lost: 0 bytes in 0 blocks
  ==18994==    indirectly lost: 0 bytes in 0 blocks
  ==18994==      possibly lost: 129 bytes in 1 blocks
  ==18994==    still reachable: 4 bytes in 1 blocks
  ==18994==         suppressed: 0 bytes in 0 blocks
  ==18994== Rerun with --leak-check=full to see details of leaked memory
  ==18994== 
  ==18994== For counts of detected and suppressed errors, rerun with: -v
  ==18994== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
  Aborted
  anisha@linux-y3pi:~/Desktop&gt;
  </code></pre>