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 readfirstline(const string&); string handlereaderror(std::exception&);

// Construct a new function: auto firstlinereader = attempt(readfirstline, handlereaderror);

// Usage: auto firstline = firstline_reader("input.txt");

if(firstline) cout << *firstline << '\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<T> 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

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

  • Comments

    • jwilliamson
      about 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!