Felspar Exceptions

Annotated exceptions for C++ 20

These exceptions allow for capture of source location information (typically the throw site). Some exceptions may also capture other information.

Basic usage

To use add the felspar-exceptions and felspar-test libraries to your project and include the utility header:

#include <felspar/exception.hpp>

For all of the exception types an extra source location can be explicitly provided to customise the captured source location:

template<typename V> inline
V const &at(std::vector<V> const &v, std::size_t const pos,
        felspar::source_location const &loc = felspar::source_location::current()) {
    if (pos >= v.size()) {
        throw felspar::stdexcept::overflow_error{
                "Requested index beyond vector bound",
                pos, v.size(), loc};
    } else {
        return v[pos];

The source location will be now be reported as the call site of the at function rather than where the exception is thrown.

All wrappers for std:: exceptions are in the felspar::stdexcept:: namespace.

felspar::stdexcept::length_error, felspar::stdexcept::logic_error, felspar::stdexcept::runtime_error and felspar::stdexcept::system_error

#include <felspar/exceptions.hpp>

Mirrors the standard library types and includes source location information. They can still be caught as their standard types, std::length_error or std::logic_error or std::runtime_error or std::system_error, and the source location will be in the what() string.

throw felspar::stdexcept::runtime_error{"An error message"};

Might be reported as:

An error message

felspar::stdexcept::overflow_error and felspar::stdexcept::underflow_error

#include <felspar/exception.hpp> // convenience header
#include <felspar/exceptions/overflow_error.hpp> // specific header
#include <felspar/exceptions/underflow_error.hpp> // specific header

Mirrors the standard library types but includes source location together with value information. They can also be used as a drop in replacement for std::overflow_error and std::underflow_error and can be caught as such, where it will only add the source location information to the what() string.

throw felspar::stdexcept::overflow_error{"Too large", 3};

Might be reported as:

Too large
Value is 3

Where an overflow is used to report a value that is too large, the limit value can also be given:

throw felspar::stdexcept::overflow_error{"Wrong number of wheels", 5, 4};

The report will look like:

Wrong number of wheels
Limit 4 and value is 5

Similarly with felspar::stdexcept::underflow_error:

throw felspar::stdexcept::underflow_error{"Too small", 3};


Too small
Value is 3


throw felspar::stdexcept::overflow_error{"Too few items", 4, 6};


Too few items
Limit 6 and value is 4


#include <felspar/exception.hpp> // convenience header
#include <felspar/exceptions/bad_alloc.hpp> // specific header

This standard version of this exception takes no arguments, but the throw location is still captured by felspar::stdexcept::bad_alloc.

Throwing the exception normally:

throw felspar::stdexcept::bad_alloc{};

Results in:


The exception is also extended to take an optional error message:

throw felspar::stdexcept::bad_alloc{"Allocation error message"};

Results in:

Allocation error message