Skip to content

Exception Handling

Yves Ineichen edited this page Oct 11, 2016 · 1 revision

Exception Handling

In all the C++ code we should make use of the exception facility. The implementation and macros can be found in utility/exception.hpp.

To throw exception the SKYLARK_THROW_EXCEPTION macro should be used, for example:

try {
  // ...
} catch (Exception e) {
  SKYLARK_THROW_EXCEPTION (
    utility::elemental_exception()
    << utility::error_msg(e.what()) );
}

The macro takes one of the predefined exception types as an argument and can be decorated with things like error messages. We currently support the following exception types:

Name Description
skylark_exception Base class exception that should be thrown if non of the below apply.
elemental_exception A call to Elemental throws an exception
unsuppored_matrix_distribution User specified a not yet supported Elemental matrix distribution
combblas_exception A call to CombBLAS throws an exception
sketch_exception The sketch layer encounters an invalid state
allocation_exception Memory could not be allocated

Adding Exception Tags

New exception tags can easily be added in the exception header, e.g.:

struct NEW_exception : virtual skylark_exception  {
public:
  using sketch_exception::operator<<;

  allocation_exception() {
    *this << error_code(NEW_NUMBER);
  }
};

where NEW has to be replaced with a new label and NEW_NUMBER with the next free error code (see errmsg[]).

Helpers

Using the two helper macros:

  • SKYLARK_PRINT_EXCEPTION_DETAILS(ex)
  • SKYLARK_PRINT_EXCEPTION_TRACE(ex)

taking an exception as an argument, more information about the exception can be printed to stderr.

CAPI

The CAPI catches all the exceptions and forwards the errors as integer error numbers back to the calling instance. With help of the const char* skylark_strerror(int error_code) method, errors can be resolved to human readable strings.

int errno = call_some_capi_func();
if(errno > 0)
  std::cout << "Encountered an error while calling CAPI: " << skylark_strerror(errno) << std::endl;

The skylark_strerror method is also available through the Python layer:

print cskylark._strerror(ERR_NR)
Clone this wiki locally