Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

rethrow_if_nested

函数   <exception>

template <class T>
  void rethrow_if_nested (const T& e);

如果嵌套重新抛出
当(且仅当)e公开且明确地派生自nested_exception时,抛出嵌套在e中的异常。

否则,函数什么也不做(也不会抛出)。
如果嵌套异常为空,则函数调用terminate函数。

☲  参数


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

☂ 异常安全性



函数可能会抛出异常。

🍄  另请参阅



throw_with_nested 抛出嵌套异常(函数)
nested_exception 嵌套异常 (类)

联系我们 免责声明 关于CandCplus 网站地图