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

std::

<nested_exception>

类   <exception>
class nested_exception;

嵌套异常
用于异常类的组件,该组件捕获当前已处理的嵌套异常。

派生自该类和其他异常类的类,可以保存当前处理的异常(嵌套异常)和其他异常(外部异常)的属性。

具有嵌套异常的对象通常通过使用外部异常对象作为参数调用throw_with_nested来构造。返回的对象具有与外部异常相同的属性和成员, 但带有与嵌套异常相关的附加信息,并包含两个成员函数来访问嵌套异常:nested_ptr和rethrow_nested。

它被声明为:
class nested_exception {
public:
  nested_exception() noexcept;
  nested_exception (const nested_exception&) noexcept = default;
  nested_exception& operator= (const nested_exception&) noexcept = default;
  virtual ~nested_exception() = default;

  noreturn void rethrow_nested() const;
  exception_ptr nested_ptr() const noexcept;
}

☞ 成员函数



(constructor) 嵌套异常构造函数 (公众成员函数)
rethrow_nested 嵌套异常重新抛出(公众成员函数)
nested_ptr 获取嵌套异常的exception_ptr(公共成员函数)

☣  示例



// nested_exception 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


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