How to use boost::error_info correctly?

后端 未结 3 928
一整个雨季
一整个雨季 2021-01-04 10:26

I\'m trying to following the examples on this page:

http://www.boost.org/doc/libs/1_40_0/libs/exception/doc/motivation.html

The minute I try the following li

相关标签:
3条回答
  • 2021-01-04 11:07

    try:

    #include <boost/exception/error_info.hpp>
    #include <errno.h>
    
    throw file_read_error() << boost::errinfo_errno(errno);
    

    even better:

    BOOST_THROW_EXCEPTION(file_read_error() << errinfo_errno(errno));
    

    Your HRESULTErrorInfo example seems correct.

    0 讨论(0)
  • 2021-01-04 11:09

    Sam Miller gave me a clue as to what the problem was. I just needed to include:

    #include <boost/exception/all.hpp>

    Thanks for your answers.

    0 讨论(0)
  • 2021-01-04 11:18
    #include <boost/exception/all.hpp>
    
    #include <boost/throw_exception.hpp>
    
    #include <iostream>
    #include <stdexcept>
    #include <string>
    
    typedef boost::error_info<struct my_tag,std::string> my_tag_error_info;
    
    int
    main()
    {
        try {
            boost::throw_exception(
                    boost::enable_error_info( std::runtime_error( "some error" ) ) 
                    << my_tag_error_info("my extra info")
                    );
        } catch ( const std::exception& e ) {
            std::cerr << e.what() << std::endl;
            if ( std::string const * extra  = boost::get_error_info<my_tag_error_info>(e) ) {
                std::cout << *extra << std::endl;
            }
        }
    }
    

    produces

    samm@macmini> ./a.out 
    some error
    my extra info
    
    0 讨论(0)
提交回复
热议问题