std::
throw_with_nested
函数模板 <exception>
noreturn template <class T>
void throw_with_nested (T&& e);
|
抛出嵌套异常
抛出一个当前处理的异常和e的组合。
当前处理的异常变成嵌套异常和外部异常e。
抛出的异常类型是公开派生自T和当前处理的异常(后者作为nested_exception组件)。
如果T是引用类型,它继承的类型是T引用的非引用类型,它应该是可复制构造的。
如果当前catch块没有处理异常,则嵌套异常为空exception_ptr。
☲ 参数
-
e
-
非联合类的对象或引用,该类不是从nested_exception派生的。
☉ 返回值
None(函数永远不会返回)。
☣ 示例
// throw_with_nested/rethrow_if_nested example
#include <iostream> // std::cerr
#include <exception> // std::exception, std::throw_with_nested, std::rethrow_if_nested
#include <stdexcept> // std::logic_error
// recursively print exception whats:
void print_what (const std::exception& e) {
std::cerr << e.what() << '\n';
try {
std::rethrow_if_nested(e);
} catch (const std::exception& nested) {
std::cerr << "nested: ";
print_what(nested);
}
}
// throws an exception nested in another:
void throw_nested() {
try {
throw std::logic_error ("first");
} catch (const std::exception& e) {
std::throw_with_nested(std::logic_error("second"));
}
}
int main () {
try {
throw_nested();
} catch (std::exception& e) {
print_what(e);
}
return 0;
}
|
输出:
second
nested: first
☂ 异常安全性
抛出一个异常。
如果参数不是正确的类型(如上所述),则调用会导致未定义的行为。
🍄 另请参阅