![](/img/trans.png)
[英]In C++,if the Base class constructor exception,then the sequence of constructor and destructor could be this?
[英]How to catch a base class constructor's exception in C++?
我有一个从另一个 class 派生的 class。 我希望能够在派生类的构造函数中捕获并重新抛出派生类的构造函数抛出的异常。
C# 中似乎有一个解决方案: https://stackoverflow.com/a/18795956/13147242
但由于没有 C# 的base
这样的关键字,我不知道在 C++ 中如何以及是否可行。
有解决办法吗? 这将使我能够重用相当多的代码。
这里有一个很好的例子: Exception is catched in a constructor try block, and handling, but still gets rethrow the second time
最终,如果您设法在派生的 class 中捕获异常,您唯一应该做的就是重新抛出该异常或抛出一个新异常。 这是因为如果基础 class 构造函数未完成,则派生的 class 将位于未定义的 state 中,您不应使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.