Proposal
Currently most of our non-throwing API, relies on bool flags and printing the precise error to the logger. While the logging is fine for user feedback, it does not allow the program to differentiate one error condition from another.
The C++11 standard includes std::error_code and std::error_condition as part of the standard library. These classes are intended to provide extensible error codes via user defined enums and custom std::error_category classes. They can be used to extend the current non-throwing API with additional error information and simplify usages that currently require 2 stage initialization to be non-throwing.
Example API
Extension of current non-throwing API.
bool foo(int a); // Old API, non-throwing.
bool foo(int a, std::error_code& ec); // New API, non-throwing.
Simplification of non-throwing 2 stage initialization.
class Foo
{
// Old API
Foo() {}
bool init(int param);
// New API, single stage initialization.
Foo(int param); // Throwing
Foo(int param, std::error_code& ec); // Non-throwing, check !ec for errors.
}
void oldUsage() {
Foo f;
if(!f.init(42)) { /* on error (generic error) */ }
}
void newUsage() {
std::error_code ec;
Foo f(42, ec);
if(ec) { /* on error ( with error value ) */}
}
Conversion of non-throwing to throwing overloads, if needed.
auto fooNoThrow(std::error_code& ec); // Non throwing overload
auto fooThrow()
{
std::error_code ec;
auto res = fooNoThrow(ec);
if(ec) {
// Throw exceptions based on the error codes.
// A utility helper method can also be provided to throw standard exceptions.
}
return res;
}
Proposal
Currently most of our non-throwing API, relies on
boolflags and printing the precise error to the logger. While the logging is fine for user feedback, it does not allow the program to differentiate one error condition from another.The C++11 standard includes
std::error_codeandstd::error_conditionas part of the standard library. These classes are intended to provide extensible error codes via user defined enums and customstd::error_categoryclasses. They can be used to extend the current non-throwing API with additional error information and simplify usages that currently require 2 stage initialization to be non-throwing.Example API
Extension of current non-throwing API.
Simplification of non-throwing 2 stage initialization.
Conversion of non-throwing to throwing overloads, if needed.