如何用合适的what()函数来补充boost::exception
How to supplement boost::exception with a proper what() function
我很喜欢boost::exception,但是我很困扰它没有提供一个合适的what()函数。现在不要感到困惑,它确实有一个很好的boost::diagnostic_information,其中包含了我想在假设的what()
函数中看到的所有信息,但由于boost::exception
不继承std::exception
,如果我多次继承(如教程中建议的,请参阅下面的行),我得到的what()
函数是std::exception
基的默认无用what()
,它解释不了异常。
struct my_exception: virtual std::exception, virtual boost::exception { };
现在显然我试图覆盖what()
,使其返回boost::diagnostic_information
,但不知何故它只是不起作用,所以我有点困惑。这可能是因为它会循环,但我不太确定。
PS:我想实现what()
正确的原因是,如果你的程序从它们死亡,默认情况下它会被很多工具显示(例如,gnu编译器会显示一个很好的致命错误,并显示what(), boost单元测试工具等)。
下面是测试代码的链接
#include <boost/exception/all.hpp>
struct my_exception: virtual std::exception, virtual boost::exception {};
struct my_exception2: virtual std::exception, virtual boost::exception {
virtual const char* what() const throw() {
return "WHAT";
}
};
struct my_exception3: virtual std::exception, virtual boost::exception {
virtual const char* what() const throw() {
return boost::diagnostic_information(this).c_str();
}
};
int main() {
try {
BOOST_THROW_EXCEPTION(my_exception());
} catch (const std::exception& e){
std::cout << e.what() << std::endl;
//This is useless ___ std::exception
}
try {
BOOST_THROW_EXCEPTION(my_exception());
} catch (const boost::exception& e){
std::cout << boost::diagnostic_information(e) << std::endl;
//This is what I'd like to see ___ main.cpp(39): Throw in function int main() ___ Dynamic exception type: boost::exception_detail::clone_impl ___ std::exception::what: std::exception
}
try {
BOOST_THROW_EXCEPTION(my_exception2());
} catch (const std::exception& e){
std::cout << e.what() << std::endl;
//Overriding what usually works ___ WHAT
}
try {
BOOST_THROW_EXCEPTION(my_exception3());
} catch (const std::exception& e){
std::cout << e.what() << std::endl;
//But somehow here it does not work ___ Unknown exception.
}
}
首先,boost::diagnostic_information
接受(const)引用异常,this
是指针:
return boost::diagnostic_information(*this).c_str();
^-- here
第二,一旦你解决了这个问题,正如你正确预测的那样,当boost::diagnostic_information
调用std::exception::what()
时,这将导致无限递归。可以使用guard成员或类似的东西来解决这个问题:
struct my_exception3: std::exception, boost::exception {
mutable bool in_what = false;
virtual const char* what() const throw() {
struct g { bool &b; ~g() { b = false; } } guard{in_what};
return in_what ? "WHAT" : (in_what = true, boost::diagnostic_information(*this).c_str());
}
};
最后,您使用的c_str
来自一个被析构的临时string
。我将把这个问题的解决方法留给练习。
冠军是…
namespace boost {
char const * diagnostic_information_what( boost::exception const & e ) throw();
}
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- boost::进程间消息队列引发错误
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- cmake如何在fedora工作站中找到boost静态库包
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- Boost Graph Library,修复节点大小
- 什么是"#include <boost/functional/hash.hpp> "?
- 基于boost的程序的静态链接——zlib问题
- C++:如何在CLion IDE中安装Boost
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- C++ CMake 构建错误:未定义对"boost::throw_exception(std::exception const&)"的引用
- 如何在 boost::exception 中迭代所有error_info?
- 将多个相同类型的boost::error_infos添加到boost::exception中
- 获取 boost::exception 的 what() 消息
- boost::exception-如何打印详细信息
- 从boost::exception和std::runtime_error继承自定义异常类
- 当 Boost.Exception 符号出现在'nm'输出中时,这意味着什么?它们会导致违反 ODR 吗?
- boost::exception 和 std::exception 之间的关系
- Catch Boost exception
- 如何用合适的what()函数来补充boost::exception