C++ 从boost::exception和std::runtime\u错误继承自定义异常类
我想介绍一个自定义异常类的层次结构,它派生自boost::exception和std::runtime\u error,以便C++ 从boost::exception和std::runtime\u错误继承自定义异常类,c++,boost,C++,Boost,我想介绍一个自定义异常类的层次结构,它派生自boost::exception和std::runtime\u error,以便what()返回一些有意义的内容 到目前为止,我没有运气: #include <iostream> #include <stdexcept> #include <boost/exception/all.hpp> typedef boost::error_info<struct tag_foo_info, unsigned lon
what()
返回一些有意义的内容
到目前为止,我没有运气:
#include <iostream>
#include <stdexcept>
#include <boost/exception/all.hpp>
typedef boost::error_info<struct tag_foo_info, unsigned long> foo_info;
struct foo_error : virtual boost::exception, virtual std::runtime_error
{
explicit foo_error(const char *const what)
: std::runtime_error(what)
{ }
};
static void foo()
{
BOOST_THROW_EXCEPTION(foo_error("foo error") << foo_info(100500));
}
int main(int argc, char *argv[])
{
try
{
foo();
}
catch (const std::exception& e)
{
std::cerr << boost::diagnostic_information(e);
return 1;
}
return 0;
}
但那不是我真正想要的。基本上,我希望一个异常类是
catch
可以通过catch(const std::exception&e)
,catch(const std::runtime\u error&e)
,catch(const boost::exception&e)
和catch(const foo\u error&e)
。可能吗?提前感谢您。std::runtime\u错误已从std::exception继承。因此,您只需要继承std::runtime\u error,就可以同时得到这两个错误
更新:
我的意思是只继承std::runtime\u错误。如果您尝试以下方法:
#include <iostream>
#include <stdexcept>
struct foo_error : public std::runtime_error
{
explicit foo_error(const char *const what)
: std::runtime_error(what)
{ }
};
static void foo()
{
throw foo_error("foo error");
}
int main(int argc, char *argv[])
{
try
{
foo();
}
catch (const std::exception& e)
{
std::cerr << boost::diagnostic_information(e);
return 1;
}
return 0;
}
#包括
#包括
结构foo_错误:public std::runtime_错误
{
显式foo_错误(常量字符*常量内容)
:std::运行时错误(什么)
{ }
};
静态void foo()
{
抛出foo_错误(“foo错误”);
}
int main(int argc,char*argv[])
{
尝试
{
foo();
}
捕获(const std::exception&e)
{
您需要公共继承
struct Exception : public boost::exception, public std::runtime_error
{
Exception()
: std::runtime_error("Hello World")
{}
};
int main()
{
try {
try {
throw Exception();
}
catch(const std::runtime_error&) {
std::cout << "std::runtime_error" << std::endl;
throw;
}
}
catch(const boost::exception&) {
std::cout << "boost::exceptionr" << std::endl;
}
return 0;
}
由于虚拟继承,最派生的类负责初始化基类(clone_impl不负责)我实际上是从发布的示例中的std::runtime\u error
继承的,但它不会编译。我做错了什么?这个答案似乎混淆了std::exception
和boost::exception
。它们不是一回事。继承std::runtime\u error
不会自动从boost::excep>继承操作
。你能解释一下为什么以及如何工作吗?我的大脑很痛。我的意思是,你刚刚用public
替换了virtual
。有什么区别?为什么现在可以工作?后果如何?我很难理解。boost异常库有一个从你的异常派生的类。
struct Exception : public boost::exception, public std::runtime_error
{
Exception()
: std::runtime_error("Hello World")
{}
};
int main()
{
try {
try {
throw Exception();
}
catch(const std::runtime_error&) {
std::cout << "std::runtime_error" << std::endl;
throw;
}
}
catch(const boost::exception&) {
std::cout << "boost::exceptionr" << std::endl;
}
return 0;
}
Throw in function void foo()
Dynamic exception type: boost::exception_detail::clone_impl<foo_error>
std::exception::what: foo error
[tag_foo_info*] = 100500
// Curiously recurring template pattern (exception.hpp:419:20)
class clone_impl: public Exception, public clone_base;