4
struct wrapper_exception {
std::runtime_error& err;
wrapper_exception(std::runtime_error& _e) : err(e) {}
};
try {
throw std::runtime_error("foo");
} catch (std::runtime_error& err) {
throw wrapper_exception(err);
}
問題:它是安全的訪問內部wrapper_exception到runtime_error它處理後的參考?
這也是我的解釋。另外一種方法是使用Boost Exception,如http://www.boost.org/doc/libs/1_48_0/libs/exception/doc/tutorial_enable_error_info.html - 也就是讓Boost幫助你複製原始異常併爲其添加額外的信息,而不是OP的包裝器實現。 – 2011-12-27 00:14:04
我也認爲這是正確的解釋。 C++ 11確實添加了一個exception_ptr和一些專門處理重新拋出的專業類。但是,不使用這些類只有'throw'可以保留原始異常。 – 2011-12-27 06:09:16