A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/system_error/error_category/ below:

abstract class

<system_error>

std::error_category

Error category

This type serves as a base class for specific category types.

Category types are used to identify the source of an error. They also define the relation between error_code and error_condition objects of its category, as well as the message set for error_code objects.

Objects of these types have no distinct values and are not-copyable and not-assignable, and thus can only be passed by reference. As such, only one object of each of these types shall exist, each uniquely identifying its own category: all error codes and conditions of a same category shall return a reference to same object.

Some standard objects are of a type derived from this class. A reference to them can be obtained by calling a specific global function for each:



Member functions (non-virtual)
(constructor)
Construct error_category (public member function)
relational operators
Relational operators (public member function)

Virtual member functions
(destructor)
Destroy error_category (public virtual member function)
name
Return category name (public pure virtual member function)
default_error_condition
Default error condition (public virtual member function)
equivalent
Check error code equivalence (public virtual member function)
message
Error message (public pure virtual member function)

Example To create a custom error category, a program shall derive from the error_category base class and override the virtual functions as needed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// custom error_category example
#include <iostream>       // std::cout, std::endl
#include <system_error>   // std::is_error_condition_enum, std::error_category,
                          // std::error_condition, std::error_code
#include <string>         // std::string

// custom error conditions enum type:
enum class custom_errc { success=0, client_error, server_error, other };
namespace std {
template<> struct is_error_condition_enum<custom_errc> : public true_type {};
}

// custom category:
class custom_category_t : public std::error_category {
public:
  virtual const char* name() const { return "custom"; }
  virtual std::error_condition default_error_condition (int ev) const {
    if ((ev>=200)&&(ev<300)) return std::error_condition(custom_errc::success);
    else if ((ev>=400)&&(ev<500)) return std::error_condition(custom_errc::client_error);
    else if ((ev>=500)&&(ev<600)) return std::error_condition(custom_errc::server_error);
    else return std::error_condition(custom_errc::other);
  }
  virtual bool equivalent (const std::error_code& code, int condition) const {
    return *this==code.category() &&
           static_cast<int>(default_error_condition(code.value()).value())==condition;
  }
  virtual std::string message(int ev) const {
    switch (ev) {
      case 200: return "OK";
      case 403: return "403 Forbidden";
      case 404: return "404 Not Found";
      case 500: return "500 Internal Server Error";
      case 503: return "503 Service Unavailable";
      default: return "Unknown error";
    }
  }
} custom_category;

// make_error_code overload to generate custom conditions:
std::error_condition make_error_condition (custom_errc e) {
    return std::error_condition(static_cast<int>(e), custom_category);
}

int main()
{
  // let's generate a 404:
  std::error_code e (404, custom_category);

  if (e == custom_errc::success) std::cout << "Success: " << e.message();
  else if (e == custom_errc::client_error) std::cout << "Client Error: " << e.message();
  else if (e == custom_errc::server_error) std::cout << "Server Error: " << e.message();
  else std::cout << "Unknown";
  std::cout << std::endl;

  return 0;
}

Output:
Client Error: 404 Not Found


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4