std::system_error

来自cppreference.com
< cpp‎ | error
 
 
 
 
 
在标头 <system_error> 定义
class system_error;
(C++11 起)

std::system_error 是多种库函数(通常是与 OS 设施交接的函数,例如 std::thread 的构造函数)所抛出的异常类型,该异常与一个相关的 std::error_code 并可能予以报告。

cpp/error/exceptioncpp/error/runtime errorstd-system error-inheritance.svg
关于这幅图像

继承图

目录

[编辑] 成员函数

构造 system_error 对象
(公开成员函数) [编辑]
替换 system_error 对象
(公开成员函数) [编辑]
返回错误码
(公开成员函数) [编辑]
[虚]
返回解释性字符串
(虚公开成员函数) [编辑]

继承自 std::exception

成员函数

销毁该异常对象
(std::exception 的虚公开成员函数) [编辑]
[虚]
返回解释性字符串
(std::exception 的虚公开成员函数) [编辑]

[编辑] 示例

#include <iostream>
#include <system_error>
#include <thread>
 
int main()
{
    try
    {
        std::thread().detach(); // attempt to detach a non-thread
    }
    catch(const std::system_error& e)
    {
        std::cout << "Caught system_error with code "
                     "[" << e.code() << "] meaning "
                     "[" << e.what() << "]\n";
    }
}

可能的输出:

Caught system_error with code [generic:22] meaning [Invalid argument]