Namespaces
Variants

std::system_error:: system_error

From cppreference.net
Utilities library
system_error ( std:: error_code ec ) ;
(1) (自 C++11 起)
system_error ( std:: error_code ec, const std:: string & what_arg ) ;
(2) (自 C++11 起)
system_error ( std:: error_code ec, const char * what_arg ) ;
(2) (自 C++11 起)
system_error ( int ev, const std:: error_category & ecat ) ;
(3) (自 C++11 起)
system_error ( int ev, const std:: error_category & ecat,
const std:: string & what_arg ) ;
(4) (自 C++11 起)
system_error ( int ev, const std:: error_category & ecat,
const char * what_arg ) ;
(4) (自 C++11 起)
system_error ( const system_error & other ) noexcept ;
(5) (自 C++11 起)

构造新的系统错误对象。

1) 构造带有错误码 ec
2) 构造包含错误码 ec 和说明字符串 what_arg 的对象。 what() 返回的字符串保证包含 what_arg 作为子字符串。
3) 构造具有底层错误码 ev 和关联错误类别 ecat 的对象。
4) 构造具有底层错误码 ev 、关联错误类别 ecat 及说明字符串 what_arg 的异常对象。 what() 返回的字符串保证包含 what_arg 作为子字符串(假设其中不包含嵌入的空字符)。
5) 复制构造函数。使用 other 的内容进行初始化。若 * this other 均具有动态类型 std::system_error ,则满足 std:: strcmp ( what ( ) , other. what ( ) ) == 0

参数

ec - 错误代码
ev - ecat 关联的枚举中的底层错误代码
ecat - 错误类别
what_arg - 说明性字符串
other - 要复制的另一个 system_error 对象

示例

演示如何从 errno 值创建 system_error 异常。

#include <iostream>
#include <system_error>
int main()
{
    try
    {
        throw std::system_error(EDOM, std::generic_category(), "FIX ME");
    }
    catch (const std::system_error& ex)
    {
        std::cout << "code:    [" << ex.code() << "]\n"
                     "message: [" << ex.code().message() << "]\n"
                     "what:    [" << ex.what() << "]\n";
    }
}

可能的输出:

code:    [generic:33]
message: [Numerical argument out of domain]
what:    [FIX ME: Numerical argument out of domain]