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:
default_error_condition
in error_category
, and how?error_codes
to the proper error_conditions
,error_condition
instances?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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With