2011-09-23 97 views
1

在我的内部日志记录库中,我试图更改自定义异常类以从boost :: exception而不是std :: exception派生。我正在这样做,以便我可以使用单个catch块来同时处理boost异常和我的应用程序异常。但是我在记录时遇到了一个问题。记录boost :: exception,同时避免文件/行/函数和唠叨

在使用boost :: diagnostic_information()记录异常时,我得到了整个9码的抛出位置。这些信息对我来说是多余的,因为我的自定义类已经以我想要的方式收集并使用了这些信息。我不想在日志中打印源代码文件/行/函数信息。

如果我定义BOOST_EXCEPTION_DISABLE或者不使用BOOST_THROW_EXCEPTION,它打印“投掷位置未知(考虑使用BOOST_THROW_EXCEPTION)”每次我登录异常。

但我怎么逃避这个唠叨?

回答

4

好吧,我想我自己找到了答案。首先,我不必从boost :: exception派生我的类,我仍然可以继续从std :: exception派生。不过,我应该使用BOOST_THROW_EXCEPTION抛出我的std :: exception派生类。因此它在起飞时变成boost :: exception。 :-)

在两者之间,我可以添加更多的信息,如果需要通过捕捉和重新投掷。

typedef boost::error_info<struct tag_errmsg, std::string> exceptionInfo; 

    catch (boost::exception &e) 
    { 
    e << exceptionInfo("some more exception data"); 
    throw; 
    } 

然后我终于可以赶上并打印这种方式:

catch (boost::exception &e) 
{ 
    std::exception const * se = dynamic_cast<std::exception const *>(&e); 
    if(se) 
    { 
     // will enter here only for my application exception and not for pure boost exception 
     std::cout << "STD: " << se->what(); 
    } 
    std::cout << " BOOST: " << *boost::get_error_info<exceptionInfo>(e);  } 
} 

这样,我将得到的std ::例外,无论是什么()字符串,并从提升的错误信息: :例外。我在正确的轨道上吗?

0

难道你不想使用exception :: what()而不是boost :: diagnostic_information()吗?

+1

boost:exception中没有what()函数。 – Sharath

相关问题