Search code examples
c++boostboost-program-options

Catching exceptions from boost::program_options


Stupid question (but I don't find anything on boost.org): how do I catch exceptions from boost::program_options? Invoking

#include <boost/program_options.hpp>
#include <iostream>
int main(int argc, char** argv){
    bool opt;
    namespace bpo = boost::program_options;
    bpo::options_description options("Allowed options");
    options.add_options()
        ("opt", bpo::bool_switch(&opt));

    bpo::variables_map options_vm;
    try {
        bpo::store(bpo::parse_command_line(argc, argv, options), options_vm);
        bpo::notify(options_vm);
    }
    catch(const std::exception &e){
        std::cerr << e.what() << std::endl;
        return 1;
    }
    return 0;
}

with any option but --opt yields

libc++abi: terminating due to uncaught exception of type boost::wrapexcept<boost::program_options::unknown_option>: unrecognised option '--optnsdf'

Why doesn't catch(const std::exception &e) catch the exception? How do I properly catch catch(const boost::wrapexceptboost::program_options::unknown_option& e)is not intended, also because there can be other program options-related exceptions, such asboost::wrapexceptboost::program_options::invalid_command_line_syntax`, and probably others.

Is there any class hierarchy for boost program options exceptions available online?


Solution

  • You can catch the base exception class program_options::error by reference:

    Live On Coliru

    #include <array>
    #include <iostream>
    
    #include <boost/program_options.hpp>
    namespace po = boost::program_options;
    
    int main() {
        std::array argv{"demo", "--oops", static_cast<char const*>(nullptr)};
    
        po::options_description options("Allowed options");
        bool opt;
        options.add_options()("opt", po::bool_switch(&opt));
    
        po::variables_map vm;
        try {
            store(parse_command_line(argv.size() - 1, argv.data(), options), vm);
            notify(vm);
        } catch (po::error const& e) {
            std::cerr << e.what() << std::endl;
            options.print(std::cerr);
            return 1;
        }
    }
    

    Printing

    unrecognised option '--oops'
    Allowed options:
      --opt 
    

    Although there's no diagram there is a kind of error reference: https://www.boost.org/doc/libs/1_84_0/doc/html/program_options/reference.html#header.boost.program_options.errors_hpp

    The individual types do mention purpose e.g.: "Base class for all errors in the library")