Sometimes, you want a flexible way to handle certain situations involving C++ exceptions, particularly those for which alternative actions are either easily encapsulated, do not substantially interrupt the program flow, or must cross a foreign-function interface or thread boundary.

A useful idiom is to rewrite attempts to perform an action with such forms of exception handling as instead returning a monadic value, like with C++17's std::optional<>:

// Given:
string read_first_line(const string&);
string handle_read_error(std::exception&);

// Construct a new function:
auto first_line_reader = attempt(read_first_line, handle_read_error);

// Usage:
auto first_line = first_line_reader("input.txt");

if(first_line)
 cout << *first_line << '\n';

It's useful to compare this with both traditional exception handling and Lippincott functions.

For purposes of crossing FFI or thread boundaries, consider the possibility of writing a function that strictly forwards parameters into the local code, and then strictly returns it's optional value-- it never leaves any possibility of an exception leaking (which generally means a crash).

The github page is here.

Looking for hackers with the skills:

Nothing? Add some keywords!

This project is part of:

Hack Week 15

Activity

  • over 7 years ago: jwilliamson started this project.
  • over 7 years ago: jwilliamson originated this project.

  • Comments

    • jwilliamson
      over 7 years ago by jwilliamson | Reply

      Note that exceptional::attempt() is variadic-- you can write: auto fn = attempt(f, h0, h1, h2);

    Similar Projects

    This project is one of its kind!