class XX
{
public:
static unsigned s_cnt;
XX()
{
++ s_cnt;
std::cout << "C XX " << s_cnt << "\n";
if ( s_cnt > 2 )
throw std::exception();
}
//private:
~XX()
{
std::cout << "~ XX\n";
}
};
unsigned XX::s_cnt = 0;
int main()
{
try
{
XX *xx = new XX[10];
} catch ( ... )
{
std::cout << "Exc\n";
}
}
Output:
C XX 1
C XX 2
C XX 3
~ XX
~ XX
Exc
But when i remove try-catch, i see:
C XX 1
C XX 2
C XX 3
terminate called after throwing an instance of 'std::exception'
what(): std::exception
zsh: abort ./a.out
Why does C++ call the destructors in first case but not in second?