我正在爲一個容器寫一個drop-replacement,並且我試圖讓 得到所有異常保證。我目前正在編寫 ,clear
方法,並且我希望它儘可能地完成,並且始終保持容器處於一致狀態,即使其中一個析構函數拋出異常。在我完成清潔後,我還想重新拋出 這個例外,最好不要切片。何時破壞了一個異常對象(並且可以控制它)?
這給我帶來了問題;何時破壞了一個例外? 讓我們看看一次嘗試: 這個例子簡化了。
void container::clear()
{
bool had_exception = false;
std::exception* exp;
internal_set_empty(); // this cant throw
while(! internal_done())
{
try
{
internal_destruct_next(); // this might throw if T::~T() throws
}
catch(std::exception& e)
{
had_exception = true;
exp = &e;
}
}
if(had_exception)
throw *exp;
}
我希望這根本無法運行,因爲異常可能是破壞 當它被認爲是處理,而這在技術上不重新拋出。
另一個嘗試將採取異常的副本,這是 我希望將切片。
有沒有辦法延長例外的生命週期,以便我可以在以後重新拋出 ? 如果可能的話,我還希望能夠通過catch(...)
重新拋出捕獲的異常 。
http:// en。如果您的方案可行,cppreference.com/w/cpp/error/exception_ptr可能會很方便。 – Mat
@Mat'exception_ptr'看起來像我正在尋找的東西,但它似乎有一個更基本的問題。 – sp2danny