12

I try to figure out how the new system_error together with error_code, error_category and not the least the (meant to implement portable error reporting) error_condition should be used.

I think by reading boost.system I understand how I should use error_codes and error_category. The description leaves out how this is used in conjunction when throwing an exception with ´system_error`, but from the interface from that class I can guess.

class system_error : public runtime_error {
public:
  // [...]
  system_error(error_code ec, const string& what_arg);
  system_error(int ev, const error_category& ecat, const string& what_arg);
  system_error(int ev, const error_category& ecat);
  // [...]

So, I throw a system_error-exception with the right int+error_category or error_code with its error_category()-method.

But whats the way to provide the portable interface with error_condition?

Both error_code and error_category both have method default_error_condition:

class error_category {
public:
  // [...]
  virtual error_condition default_error_condition(int ev) const noexcept;
  // [...]

class error_code {
public:
  // [...]
  error_condition default_error_condition(int ev) const noexcept;
  // [...]

The error_category-Instances should be pre-created, for example (user-code):

struct AppCategory : public error_category { 
  const char *name() const noexcept override {
    return "application"; } 
  string message(int ev) const override {
    switch(ev) {
      case 14: return "error message";
      default: return "???";
    }
  } 
  error_condition default_error_condition(int ev) const noexcept override {
     ... ??? ...
  }
};

My questions implementing this:

  • Should I implement default_error_condition in error_category, and how?
  • Or how do I connect error_codes to the proper error_conditions,
  • and should I pre-construct error_condition instances?
  • A class error_code is not supposed to be provided by the user (me), right?

Is there a good example where I can take a look at code how error_condition is supposed to be extended by the user, in conjunction with system_error-exceptions?

CesarB
  • 43,947
  • 7
  • 63
  • 86
towi
  • 21,587
  • 28
  • 106
  • 187

1 Answers1

5

If the error codes for your error category can be mapped to one of the std::errc error codes then default_error_condition should do that mapping, and return a std::error_condition with a category of std::generic_category() and a value of the corresponding std::errc enum value. Otherwise it should return an error_condition referring to that category.

Anthony Williams
  • 66,628
  • 14
  • 133
  • 155
  • If I have a set of error conditions from my multi platform application , how should they be thrown? From the documentation it seems strange to put them in a `system_error` as that carries a platform dependent `error_code`. For example, I have a `enum` that is specified as `is_error_condition_enum` and I have an `error_category` that matches those error conditions. Do I need to create an exception that carries an `error_condition` in this case? – Matt Clarkson Jan 29 '13 at 13:48
  • 1
    You could create a new exception type: see for example `std::future_error` which is specific to the futures API. – Anthony Williams Jan 29 '13 at 16:03
  • Thanks. That's a great reference. Also thanks for all the work you have done on `this::thread`, `boost::thread` and `std::thread`. They are pure win. – Matt Clarkson Jan 29 '13 at 16:35