# Boost Exception

## Frequently Asked Questions

### Why doesn't boost::exception derive from std::exception?

Despite that virtual inheritance should be used in deriving from base exception types, many programmers fail to follow this principle when deriving from std::exception. If boost::exception derives from std::exception, using the enable_error_info function with such user-defined types would introduce dangerous ambiguity which would break all catch(std::exception &) statements.

Of course, boost::exception should not be used to replace std::exception as a base type in exception type hierarchies. Instead, it should be included as a virtual base, in addition to std::exception (which should also be derived virtually.)

### Why is boost::exception abstract?

To prevent exception-neutral contexts from erroneously erasing the type of the original exception when adding error_info to an active exception object:

    catch( boost::exception & e )
        {
        e << foo_info(foo);
        throw e; //Compile error: boost::exception is abstract
        }

The correct code is:

    catch( boost::exception & e )
        {
        e << foo_info(foo);
        throw; //Okay, re-throwing the original exception object.
        }

### What is the space overhead of the boost::exception base class?

The space overhead for the boost::exception data members is negligible in the context of exception handling. Throwing objects that derive from boost::exception does not by itself cause dynamic memory allocations.

Deriving from boost::exception enables any data to be added to exceptions, which usually does allocate memory. However, this memory is reclaimed when the exception has been handled, and since typically user code does not allocate memory during the unrolling of the stack, adding error info to exceptions should not cause memory fragmentation.

### Should I use boost::throw_exception or BOOST_THROW_EXCEPTION or just throw?

The benefit of calling boost::throw_exception instead of using throw directly is that it ensures that the emitted exception derives from boost::exception and that it is compatible with boost::current_exception.

The BOOST_THROW_EXCEPTION macro also results in a call to boost::throw_exception, but in addition it records in the exception object the __FILE__ and __LINE__ of the throw, as well as the pretty name of the function that throws. This has virtually no overhead, yet enables boost::diagnostic_information to compose a more useful, if not user-friendly message.

Typical use of boost::diagnostic_information is:

    catch( boost::exception & e )
        {
        std::cerr << "OMG!" << boost::diagnostic_information(e);
        }
    catch( ... )
        {
        std::cerr << "OMG!!!";
        }

This is a possible message it may display, the first line is only possible if BOOST_THROW_EXCEPTION is used:

    example_io.cpp(70): Throw in function class boost::shared_ptr<struct _iobuf> __cdecl my_fopen(const char *,const char *)
    Dynamic exception type: class boost::exception_detail::clone_impl<class fopen_error>
    std::exception::what: example_io error
    [struct boost::errinfo_api_function_ *] = fopen
    [struct boost::errinfo_errno_ *] = 2, "No such file or directory"
    [struct boost::errinfo_file_name_ *] = tmp1.txt
    [struct boost::errinfo_file_open_mode_ *] = rb

### Why is boost::exception integrated in boost::throw_exception?

The boost::throw_exception function predates the Boost Exception library and there has been some concern about its current behavior of injecting boost::exception as a base of any exception passed to boost::throw_exception. Such concerns are dictated by the typical strict interpretation of a common principle in C and C++, that users only pay for features they actually use.

The problem is that users of Boost Exception can't by themselves cause a library to throw types that derive from boost::exception, and without this they can't use any of the Boost Exception facilities.

For example, if a user wants to use Boost Serialization in a separate thread, it is desirable to be able to transport exceptions emitted by that library into the main thread where they can be analyzed to generate a user-friendly message. This can be easily achieved using boost::exception_ptr, but this requires that Boost Serialization throws exceptions using boost::enable_current_exception. If Boost Serialization calls boost::throw_exception to throw, this behavior happens automatically and transparently.

The cost of this integration is:

- In terms of space: a pointer and 3 ints are added to the static size of exception objects.
- In terms of speed: the pointer is initialized to null at the point of the throw.
- In terms of coupling: about 400 self-contained lines of C++ with no external includes.

### Why use operator<< overload for adding info to exceptions?

Before throwing an object of type that derives from boost::exception, it is often desirable to add one or more error_info objects in it. The syntactic sugar provided by exception/operator<< allows this to be done directly in a throw expression:

    throw error() << foo_info(foo) << bar_info(bar);

which saves typing compared to this possible alternative:

    error e;
    e.add(foo_info(foo));
    e.add(bar_info(bar));
    throw e;

and looks better than something like:

    throw error().add(foo_info(foo)).add(bar_info(bar));

### Why is operator<< allowed to throw?

This question is referring to the following issue. Consider this throw statement example:

    throw file_open_error() << file_name(fn);

The intention here is to throw a file_open_error, however if operator<< fails to copy the std::string contained in the file_name error_info wrapper, a std::bad_alloc could propagate instead. This behavior seems undesirable to some programmers.

Bjarne Stroustrup, The C++ Programming Language, 3rd Edition, page 371:

> "Throwing an exception requires an object to throw. A C++ implementation is required to have enough spare memory to be able to throw bad_alloc in case of memory exhaustion. However, it is possible that throwing some other exception will cause memory exhaustion."

So, an attempt to throw any exception may already result in propagating std::bad_alloc instead.

See also: Boost Exception | Motivation