Clone wiki

fungo / Home


fungo is a C++ library that is designed for those of us stuck using older C++ implementations that do not yet support std::exception_ptr.

In other words, fungo allows you to make a fair attempt at storing and later re-throwing exceptions inside a catch(...) block. This is useful for propagating exceptions across thread-joins or over C/C++ boundary interfaces.

fungo is made available under the Boost Software License version 1.0, making it suitable for use in both closed-source and open-source software.

Quick example

#include <fungo/fungo.hpp>

// First we tell a catcher about the exceptions we're interested in.
fungo::catcher mitten;
// ...

// Now we can use fungo to catch and store them
fungo::exception_cage cage;

try { throw std::logic_error("2+2=5"); }
catch (...) { mitten.store_current_exception(cage); }

// cage now stores our exception. 
cage.rethrow(); // throws a logic_error (wasn't sliced in to a runtime_error)

fungo does everything it can to ensure that exceptions aren't sliced when they're caught and copied in to an exception_cage. It does this by sorting registered exception-handlers by the size of their associated exception types (biggest to smallest) and seeing which, if any, of the existing handlers can catch an instance of the exception being registered.

By doing this, fungo is able to attempt to catch the most-derived exceptions first thereby keeping the slicing to a minimum.

fungo also comes with a function called raise() which may be used instead of a regular C++ throw statement:

fungo::raise(std::runtime_error("oops")); // throws a runtime_error catchable by any catcher object

By using raise(), you ensure that whatever you throw can be caught and stored in a fungo::exception_cage by any fungo::catcher, regardless of whether or not the catcher has been told about the type of exception raise()d.