【发布时间】:2011-12-29 12:12:11
【问题描述】:
请告诉我如何在 boost::exception 中正确使用 try/catch。
这是一个例子
void Settings::init(const std::string &filename)
{
using boost::property_tree::ptree;
try
{
read_xml(filename, pt);
}
catch(boost::exception const& ex)
{
LOG_FATAL("Can't init settings. %s", /* here is the question */);
}
}
我也需要 catch std::exception 吗? 我不能让我的应用程序失败,所以我只需要记录所有内容。
统一更新: 我现在也无法理解如何从异常中检索日志记录信息???
【问题讨论】:
-
是的,我也会在 boost::exception 之后添加 std::exception。我还要添加 catch (...)
-
如果因为发生错误而引发异常,那么仅仅捕获异常并不能消除错误。您的应用程序仍然失败,您只是更难看到发生了什么。因此,只有在您知道异常指示并能够有意义地处理它们的情况下才能捕获异常
-
@jalf 上面的代码并没有简单地吞下异常。至少有一些日志记录。
-
Guys =) 现在这不是那么重要了。你最好告诉我关于 UPD 的事
-
UPD?什么是 UPD?我认为在一个关于如何正确捕获异常的问题中,讨论应该如何捕获异常对我们来说非常重要。 ;)
标签: c++ boost logging exception-handling