An exception gets thrown twice from a constructor with a function-try-block

后端 未结 3 1716
眼角桃花
眼角桃花 2021-01-05 20:13

Why does the following exception thrown from the constructor of class A get caught twice, first by the catch within the constructor itself and second time by the catch in th

3条回答
  •  独厮守ぢ
    2021-01-05 21:04

    You are utilizing a feature called function-try-catch. When used in a constructor, it allows catching exceptions in the initialization list (especially useful for catching exceptions in base class constructors) as well as the constructor body. But since the exception is thrown in a constructor, the class is incomplete, so the compiler automatically rethrows any caught exception. that is why you see it caught twice.

    Read the following article for more details:

    Constructors and Exception in C++

提交回复
热议问题