GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM
  • C library:
  • <cassert> (assert.h)
  • <cctype> (ctype.h)
  • <cerrno> (errno.h)
  • <cfenv> (fenv.h)
  • <cfloat> (float.h)
  • <cinttypes> (inttypes.h)
  • <ciso646> (iso646.h)
  • <climits> (limits.h)
  • <clocale> (locale.h)

This view

  • Original page
  • New address

GeezerVison is showing cplusplus.com — std::error_category

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Reference

  • C library:

    • <cassert> (assert.h)
    • <cctype> (ctype.h)
    • <cerrno> (errno.h)
    • C++11
      <cfenv> (fenv.h)
    • <cfloat> (float.h)
    • C++11
      <cinttypes> (inttypes.h)
    • <ciso646> (iso646.h)
    • <climits> (limits.h)
    • <clocale> (locale.h)
    • <cmath> (math.h)
    • <csetjmp> (setjmp.h)
    • <csignal> (signal.h)
    • <cstdarg> (stdarg.h)
    • C++11
      <cstdbool> (stdbool.h)
    • <cstddef> (stddef.h)
    • C++11
      <cstdint> (stdint.h)
    • <cstdio> (stdio.h)
    • <cstdlib> (stdlib.h)
    • <cstring> (string.h)
    • C++11
      <ctgmath> (tgmath.h)
    • <ctime> (time.h)
    • C++11
      <cuchar> (uchar.h)
    • <cwchar> (wchar.h)
    • <cwctype> (wctype.h)
  • Containers:

    • C++11
      <array>
    • <deque>
    • C++11
      <forward_list>
    • <list>
    • <map>
    • <queue>
    • <set>
    • <stack>
    • C++11
      <unordered_map>
    • C++11
      <unordered_set>
    • <vector>
  • Input/Output:

    • <fstream>
    • <iomanip>
    • <ios>
    • <iosfwd>
    • <iostream>
    • <istream>
    • <ostream>
    • <sstream>
    • <streambuf>
  • Multi-threading:

    • C++11
      <atomic>
    • C++11
      <condition_variable>
    • C++11
      <future>
    • C++11
      <mutex>
    • C++11
      <thread>
  • Other:

    • <algorithm>
    • <bitset>
    • C++11
      <chrono>
    • C++11
      <codecvt>
    • <complex>
    • <exception>
    • <functional>
    • C++11
      <initializer_list>
    • <iterator>
    • <limits>
    • <locale>
    • <memory>
    • <new>
    • <numeric>
    • C++11
      <random>
    • C++11
      <ratio>
    • C++11
      <regex>
    • <stdexcept>
    • <string>
    • C++11
      <system_error>
    • C++11
      <tuple>
    • C++11
      <type_traits>
    • C++11
      <typeindex>
    • <typeinfo>
    • <utility>
    • <valarray>

<system_error>

  • classes

    • C++11
      error_category
    • C++11
      error_code
    • C++11
      error_condition
    • C++11
      is_error_code_enum
    • C++11
      is_error_condition_enum
    • C++11
      system_error
  • enum classes

    • C++11
      errc
  • functions

    • C++11
      generic_category
    • C++11
      make_error_code
    • C++11
      make_error_condition
    • C++11
      system_category

error_category

  • C++11
    error_category::~error_category
  • C++11
    error_category::error_category
  • member functions

    • C++11
      relational operators (error_category)
  • virtual member functions

    • C++11
      error_category::default_error_condition
    • C++11
      error_category::equivalent
    • C++11
      error_category::message
    • C++11
      error_category::name
  • Reference
  • <system_error>
  • error_category
abstract class
<system_error>

std::error_category

class 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:
headercategory function
<system_error>generic_category
system_category
<ios>
<iostream>
iostream_category
<future>future_category

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

Home page | Privacy policy
© cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s
Spotted an error? contact us
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.