Exception slicing - is this due to generated copy constructor?

后端 未结 4 1653
抹茶落季
抹茶落季 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:15

    When you throw an object, you're actually throwing a copy of the object, not the original. Think about it - the original object is on the stack, but the stack is being unwound and invalidated.

    I believe this is part of the standard, but I don't have a copy to reference.

    The type of exception being thrown in the catch block is the base type of the catch, not the type of the object that was thrown. The way around this problem is to throw; rather than throw e; which will throw the original caught exception.

提交回复
热议问题