Search code examples
c++exceptionboostmemory-mapped-files

What exceptions does boost mapped_file_source throw?


Boost mapped_file_source seems to throw an exception in case of e.g. file not found. What exception classes exactly does it throw? It doesn't seem to say in the documentation, unless I'm missing something.

Edit: test case:

#include <boost/iostreams/device/mapped_file.hpp>

int main(int argc, char** argv) {
    boost::iostreams::mapped_file_source file;
    file.open(argv[1]);
    return 0;
}

Solution

  • In case the mapping fails, the code

    mapped_handle_ = 
            ::CreateFileMappingA( 
                handle_, 
                NULL,
                protect,
                0, 
                0, 
                NULL );
        if (mapped_handle_ == NULL)
            cleanup_and_throw("failed create mapping");
    

    will eventually throw a

    boost::iostreams::detail::throw_system_failure(msg);
    

    i.e. a subclass of std::exception. In this case it should be std::ios::failure:

    BOOST_IOSTREAMS_FAILURE

    Expands to std::ios_base::failure if available, and to a suitable derived class of std::exception otherwise.

    Reference: http://www.boost.org/doc/libs/1_41_0/libs/iostreams/src/mapped_file.cpp