#include <iostream>
class A
{
public:
A()
{
std::cout << "A()" << std::endl;
}
virtual ~A()
{
std::cout << "~A()" << std::endl;
}
};
class B:public A
{
public:
B()
{
throw std::exception();
std::cout << "B()" << std::endl;
}
~B()
{
std::cout << "~B()" << std::endl;
}
};
int main()
{
try
{
B b;
}
catch(std::exception & e)
{
}
return 0;
}
The above code outputs,
A()
~A()
By the time the exception is thrown, B has been created. Then why isn't the destructor of B being called?