Как извлечь любую информацию из boost :: errinfo_nested_exception? - PullRequest
5 голосов
/ 27 июля 2011

Я недавно начал использовать boost :: exception. Теперь я хотел бы использовать boost :: errinfo_nested_exception для вывода информации о причине ошибки. Проблема в том, что я не могу понять, как получить информацию о причине. Я пробовал следующее безуспешно:

#include <iostream>
#include <boost/exception/all.hpp>

struct myex : public virtual boost::exception {};

int main()
{
   myex cause;
   cause << boost::errinfo_file_name("causefile.cpp");

   try {
      myex ex;
      ex << boost::errinfo_nested_exception(boost::copy_exception(cause));
      throw ex;
   }
   catch (myex& e) {
      // Here I would like to extract file name from cause and print
      // it in a nice way, but I cant figure out what to do with a
      // boost::exception_ptr.
      const boost::exception_ptr* c = 
         boost::get_error_info<boost::errinfo_nested_exception>(e);

      // I cant do this:  
      // const std::string* file = boost::get_error_info<boost::errinfo_file_name>(*c);

      // Nor this: 
      // const std::string* file = boost::get_error_info<boost::errinfo_file_name>(**c);

      // This works fine and the nested exception is there, but that's not what I want.
      std::cout << boost::diagnostic_information(e) << std::endl;
   }

   return 0;
}

Ответы [ 2 ]

2 голосов
/ 27 июля 2011

Вам необходимо перебросить вложенное исключение и проверить, что:

const boost::exception_ptr* c = 
    boost::get_error_info<boost::errinfo_nested_exception>(e);
if(c) try {
    boost::rethrow_exception(*c);
} catch(boost::exception const& e) { // or a type derived from it
    const std::string* file = boost::get_error_info<boost::errinfo_file_name>(e);
    // ...
} catch(...) {
    // presumably you don't want the exception to escape if it is
    // not derived from boost::exception
}

Я лично использую оболочку get_error_info, которая возвращает результат boost::get_error_info<some_error_info>(e), или, если ничего не найдено, результат get_error_info<some_error_info>(nested) (рекурсивный вызов здесь) или 0, если нет вложенного исключения (или оно не error_info -enabled).

В качестве альтернативы / в качестве дополнения, вы можете указать код проверки выше (другойcatch пункты) в функции:

std::string const* // or return a tuple of what you examined etc.
examine_exception()
{
    try {
        throw; // precondition: an exception is active
    } catch(boost::exception const& e) {
        // as above
        return ...;
    }
}
1 голос
/ 27 июля 2011

boost :: диагностическая_информация - это правильный способ получить описание.Но вы также можете перегрузить to_string для boost :: error_info (T): http://svn.boost.org/svn/boost/trunk/boost/exception/errinfo_errno.hpp

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...