Exceptions and the nothrow_integer type

Many things can go wrong while operating on an integer, such as divide-by-zero errors. Under normal circumstances, these result in exceptions. But exceptions sometimes make your code harder to follow, or even harder to write. That's why this library includes the nothrow_integer type.

The nothrow_integer type has all of the functions that the xint::integer type does (they're generated from the same class template). When you use it, any exceptions generated by the library are caught internally, and the functions return a special value indicating failure (most often the Not-a-Number value).

Here's an example, which can be found in the examples subdirectory as exceptions.cpp:

#include <boost/xint/integer.hpp>
#include <ostream>

int main() {
    using namespace std;
    using namespace boost::xint;

    // We'll wrap this in a try block for demonstration purposes, just to
    // prove that it doesn't throw an exception. You normally don't need a
    // try block if you're using nothrow_integers.
    try {
        // Call the function(s) that may produce errors. The example call tries
        // to interpret an empty string as an integer, which would normally
        // throw an xint::exception::invalid_digit.
        nothrow_integer n = nothrow_integer("");

        // n should be Not-a-Number, because there was an error but exceptions
        // are not allowed on nothrow_integers. This example code is fairly
        // useless, but it shows how to test for errors.
        if (n.is_nan()) {
            cout << "n is Not-a-Number, do some intelligent error handling here."
                << endl;
        } else {
            // This example code should never get to this block, but you would
            // usually put the normal program flow code here.
            cout << "Error, n should be Not-a-Number and it isn't!" << endl;
        }
    } catch (std::exception& e) {
        // This example code should never get to this block, because exceptions
        // were blocked.
        cout << "Error, nothrow_integer threw an exception!\n" << e.what() <<
            endl;
    }
}

© Copyright Chad Nelson, 2010-2011. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)