Exception slicing - is this due to generated copy constructor?

后端 未结 4 1655
抹茶落季
抹茶落季 2020-12-15 10:56

I\'ve just fixed a very subtle bug in our code, caused by slicing of an exception, and I now want to make sure I understand exactly what was happening.

Here\'s our b

4条回答
  •  心在旅途
    2020-12-15 11:26

    Yes.

    throw e;
    

    throws an exception of the static type of e, regardless of whatever e actually is. In this case, the Derived exception is copied to an Exception using a copy constructor.

    In this case you can just

    throw;
    

    to get the Derived exception bubble up correctly.

    If you're interesting in polymorphical throwing in some other cases, refer to the always so useful C++ FAQ Lite.

提交回复
热议问题