) { std::throw_with_nested( std::runtime_error("Couldn't open " + s) ); } } // 示例函数,捕捉异常并将其包装于 nested_exception void run() { try { open_file("nonexistent.file"); } catch(...) { std::throw_with_nested( std::runtime_error("run() failed") ); } } // ...
template< class T > [[noreturn]] void throw_with_nested( T&& t ); (C++11 起) 若std::decay<T>::type 是非final 、非 union 、既非 std::nested_exception 亦非从 std::nested_exception 导出的类类型,则抛出一个从 std::nested_exception 和std::decay<T>::type 两者公开导出的未指定类型的...
) { std::throw_with_nested(std::runtime_error("run() failed")); } } // runs the sample function above and prints the caught exception int main() { try { run(); } catch (const std::exception& e) { print_exception(e); } } Possible output: exception: run() failed exception:...