12

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 line:

throw file_read_error() << errno_code(errno);

I get an error:

error C2440: '<function-style-cast>' : cannot convert from 'int' to 'errno_code'

How do I get this to work??

Ideally I want to create something like this:

typedef boost::error_info<struct tag_HRESULTErrorInfo, HRESULT> HRESULTErrorInfo;

But I can't even get the first examples to work.

Edit: Here is a brief example of what generates error C2440 for me:

struct exception_base: virtual std::exception, virtual boost::exception { };
struct io_error: virtual exception_base { };
struct file_read_error: virtual io_error { };

typedef boost::error_info<struct tag_errno_code,int> errno_code;

void foo()
{
    // error C2440: '<function-style-cast>' : cannot convert from 'int' to 'errno_code'
    throw file_read_error() << errno_code(errno);
}
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
  • Could you post a complete minimal example you are trying to compile when getting the C2440 error? – Gabriel Schreiber Sep 23 '10 at 21:26
  • View the example on the link. I couldn't get `throw file_open_error() << errno_code(errno);` to work. – Mark Ingram Sep 24 '10 at 08:15
  • For others encountering this: the "motivation" page in the Boost Exception docs is incomplete -- a better example is here: http://www.boost.org/doc/libs/1_56_0/libs/exception/doc/tutorial_transporting_data.html, where they list the correct headers. – Max Fellows Mar 17 '15 at 17:48

3 Answers3

17
#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
Sam Miller
  • 23,808
  • 4
  • 67
  • 87
9

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.

Community
  • 1
  • 1
Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
  • 7
    Why the hell has the correct answer been downvoted?? That #include fixes the code that I posted above. – Mark Ingram May 14 '12 at 15:15
  • An example of a template-related error for which the compiler error message is totally unrelated to the cause of the issue. Thanks for this. – Dan Nissenbaum Nov 21 '16 at 20:32
2

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.

ronag
  • 49,529
  • 25
  • 126
  • 221