lundi 27 novembre 2017

explanation for output of this snippet

Can anyone explain me why the output of the below snippet is :

MyClass constructor
catch block

why is the destructor of object m not been called as the scope of it is within try block. When it comes to catch block with "throw 0;" statement, the object m of MyClass is out of scope and its destructor should have been invoked right? Am i missing some kind of concept hear?

class MyClass {                                                                 
public:                                                                     
    MyClass() {                                                                 
        std::cout << "\nMyClass constructor" ;                                  
        throw 0;                                                                
    }                                                                           
    ~MyClass() {                                                                      
        std::cout << "\nMyClass destructor" << std::endl;                       
    }                                                                           
};                                                                              

int main(void)                                                                  
{                                                                               
    try {                                                                       
        MyClass m;                                                            
    } catch(int e) {                                                            
        std::cout << "\ncatch block" << std::endl;                              
    }                                                                           

    return 0;                                                                   
}    

Aucun commentaire:

Enregistrer un commentaire