views:

99

answers:

1

Hello all, Consider the following C++ program

struct str
{
       int mem;
       str()
       try
          :mem(0)
       {
               throw 0;
       }
       catch(...)
       {
       }
};

int main()
{
       str inst;
}

The catch block works, i.e. the control reaches it, and then the program crashes. I can't understand what's wrong with it.

Thanks in advance,

Armen

+11  A: 

Once the control reaches the end of the catch block of function-try-block of a constructor, the exception is automatically rethrown. As you don't catch it further in main(), terminate() is called. Here is an interesting reading: http://www.drdobbs.com/184401316

usta
@usta: Thanks a lot. Is it rethrown only in constructor or in other functions too?
Armen Tsirunyan
@Armen Tsirunyan: You're welcome ;) Only in constructor. That all is explained very well in the link above.
usta