- WIKI教程
- C ++标准库
- C++ Library - <exception>
C++ Library - <exception>
介绍 (Introduction)
它是标准的异常类。 标准库的组件抛出的所有对象都派生自此类。 因此,可以通过引用捕获此类型来捕获所有标准异常。
声明 (Declaration)
以下是std :: exception的声明。
class exception;
例子 (Example)
在下面的示例中为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();
}
}
派生类型
Derived types(through logic_error)
Derived types(through runtime_error)
Derived types(through bad_alloc)
派生类型(通过system_error,自C ++ 11以来)
成员函数 (Member functions)