Namespaces
Variants
Views
Actions

std::system_error

From cppreference.com
< cpp‎ | error
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (C++20)
Dynamic memory management
Program utilities
Coroutine support (C++20)
Variadic functions
Debugging support
(C++26)
Three-way comparison
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)

 
Diagnostics library
Exception handling
Exception handling failures
(until C++17*)
(until C++17*)
(C++11)(until C++17*)    
(until C++17*)
Error codes
Error codes
Exception categories
System error support
(C++11)
(C++11)
system_error
(C++11)
Assertions
Stacktrace
 
 
Defined in header <system_error>
class system_error;
(since C++11)

std::system_error is the type of the exception thrown by various library functions (typically the functions that interface with the OS facilities, e.g. the constructor of std::thread) when the exception has an associated std::error_code, which may be reported.

cpp/error/exceptioncpp/error/runtime errorstd-system error-inheritance.svg

Inheritance diagram

Contents

[edit] Member functions

constructs the system_error object
(public member function) [edit]
replaces the system_error object
(public member function) [edit]
returns error code
(public member function) [edit]
[virtual]
returns an explanatory string
(virtual public member function) [edit]

Inherited from std::exception

Member functions

[virtual]
destroys the exception object
(virtual public member function of std::exception) [edit]
[virtual]
returns an explanatory string
(virtual public member function of std::exception) [edit]

[edit] Example

#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";
    }
}

Possible output:

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