Skip to content

Enhancing non-throwing error reporting. #2135

@Dimi1010

Description

@Dimi1010

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;
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions