Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/error/system category"

From cppreference.com
< cpp‎ | error
(add notes)
m (fmt)
 
Line 1: Line 1:
 
{{cpp/title|system_category}}
 
{{cpp/title|system_category}}
 
{{cpp/error/navbar}}
 
{{cpp/error/navbar}}
{{ddcl | header=system_error | since=c++11 |
+
{{ddcl|header=system_error|since=c++11|
 
const std::error_category& system_category() noexcept;
 
const std::error_category& system_category() noexcept;
 
}}
 
}}
Line 18: Line 18:
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
 
#include <iomanip>
 
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
Line 37: Line 36:
 
     }
 
     }
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
 
Category: generic
 
Category: generic
 
Value:    33
 
Value:    33
Line 50: Line 49:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/error/dsc generic_category}}
+
{{dsc inc|cpp/error/dsc generic_category}}
{{dsc inc | cpp/error/dsc errc}}
+
{{dsc inc|cpp/error/dsc errc}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 02:46, 4 January 2024

 
 
 
Defined in header <system_error>
const std::error_category& system_category() noexcept;
(since C++11)

Obtains a reference to the static error category object for errors reported by the operating system. The object is required to override the virtual function std::error_category::name() to return a pointer to the string "system". It is also required to override the virtual function std::error_category::default_error_condition() to map the error codes that match POSIX errno values to std::generic_category.

Contents

[edit] Parameters

(none)

[edit] Return value

A reference to the static object of unspecified runtime type, derived from std::error_category.

[edit] Notes

On Windows, system_category() typically maps some Windows error codes to POSIX ones. On POSIX, system_category() tends to be equivalent to std::generic_category() except for the name.

[edit] Example

#include <iomanip>
#include <iostream>
#include <string>
#include <system_error>
 
int main()
{
    for (int const code : {EDOM, 10001})
    {
        const std::error_condition econd =
            std::system_category().default_error_condition(code);
 
        std::cout << "Category: " << econd.category().name() << '\n'
                  << "Value:    " << econd.value() << '\n'
                  << "Message:  " << econd.message() << "\n\n";
    }
}

Possible output:

Category: generic
Value:    33
Message:  Numerical argument out of domain
 
Category: system
Value:    10001
Message:  Unknown error 10001

[edit] See also

identifies the generic error category
(function) [edit]
(C++11)
the std::error_condition enumeration listing all standard <cerrno> macro constants
(class) [edit]