C++ 库 - <异常>
介绍
它是一个标准的异常类。标准库组件抛出的所有对象均派生自此类。因此,所有标准异常都可以通过引用捕获该类型来捕获。
宣言
以下是 std::exception 的声明。
class exception;
例子
在下面的 std::Exception 示例中。
#include <thread>
#include <vector>
#include <iostream>
#include <atomic>
 
std::atomic_flag lock = ATOMIC_FLAG_INIT;
 
void f(int n) {
   for (int cnt = 0; cnt < 100; ++cnt) {
      while (lock.test_and_set(std::memory_order_acquire))
         ;
      std::cout << "Output from thread " << n << '\n';
      lock.clear(std::memory_order_release);
   }
}
 
int main() {
   std::vector<std::thread> v;
   for (int n = 0; n < 10; ++n) {
      v.emplace_back(f, n);
   }
   for (auto& t : v) {
      t.join();
   }
}
派生类型
派生类型(通过logic_error)
| 先生。 | 派生类型 | 定义 | 
| 1 | 域错误 | 这是一个域错误异常 | 
| 2 | 未来错误 | 这是未来的错误异常 | 
| 3 | 无效的论点 | 这是一个无效参数异常 | 
| 4 | 长度错误 | 这是一个长度错误异常 | 
| 5 | 超出范围 | 这是一个超出范围的异常 | 
派生类型(通过runtime_error)
| 先生。 | 派生类型 | 定义 | 
| 1 | 溢出错误 | 这是一个溢出错误异常 | 
| 2 | 范围错误 | 这是一个范围错误异常 | 
| 3 | 系统错误 | 这是系统错误异常 | 
| 4 | 下溢错误 | 这是一个下溢错误异常 | 
派生类型(通过 bad_alloc)
| 先生。 | 派生类型 | 定义 | 
| 1 | 坏数组新长度 | 这是数组长度错误的例外情况 | 
派生类型(通过 system_error,自 C++11 起)
会员功能