Dec 2, 2013

[C++][NOTE][ORIGINAL] Exception & RAII object destructor

Destructors of RAII objects are not invoked if a thrown exception will never be caught,
because in this scenario no exception handler is installed.

Also, if a destructor leaks out an exception while another exception is in flight,
then all bets are off.

 
#include <iostream>                                                                       
using namespace std;                                                                      
                                                                                          
struct Test                                                                               
{                                                                                         
   ~Test(){                                                                               
      cout << "ha" << endl;                                                               
   }                                                                                      
};                                                                                        
                                                                                          
void ha()                                                                                 
{                                                                                         
   Test t;                                                                                
   throw 0;                                                                               
}                                                                                         
                                                                                          
int main(){                                                                               
   try{                                                                                   
      ha();                                                                               
   }                                                                                      
   catch(...){                                                                            
      cout << "catch!" << endl;                                                           
   }                                                                                      
                                                                                          
}   

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.