12

考虑以下使用 boost 异常类的代码:

class exception : virtual public boost::exception {
    // ...
};

template<typename Exc>
class exception_impl : virtual public std::exception
                     , public Exc {
public:
    exception_impl(const Exc& exc) : Exc(exc) {}
    virtual const char* what() const throw() {return "blah";}
};

(实际上,这段代码更复杂。例如,exception_impl仅派生于std::exception后者还不是 . 的直接或间接基类Exc。但这只会分散我对问题的注意力,所以我跳过了它。)


鉴于此,我现在可以派生自己的异常类:

class some_exception : public exception {
    // ...
};

并使用它们:

struct tag_test_int;
typedef boost::error_info<tag_test_int,int> test_int_info;

void f()
{
    boost::throw_exception( exception_impl<some_exception>() << test_int_info(42) );
}

然而,事实证明,产生的异常没有test_int_info对象。所以我更改了exception_impl构造函数以提供一些诊断信息:

    exception_impl(const Exc& exc)
    : Exc(exc) {
        std::cerr << "========================================================================\nexc:\n";
        std::cerr << boost::diagnostic_information(exc);
        std::cerr << "========================================================================\n*this:\n";
        std::cerr << boost::diagnostic_information(*this);
        std::cerr << "========================================================================\n";
    }

这确实表明,当我将Exc对象复制到exception_impl基类对象中时,信息会丢失:

==================================================== =======================
除:
抛出位置未知(考虑使用 BOOST_THROW_EXCEPTION)
动态异常类型:some_exception
[tag_test_int*] = 42
==================================================== =======================
*这:
抛出位置未知(考虑使用 BOOST_THROW_EXCEPTION)
动态异常类型:exception_impl
标准::异常::什么:“废话”

IIRC,异常对象必须根据标准是可复制的,并且忽略可能的优化,复制抛出表达式的结果。所以 boost 的异常必须是可复制的,并且它们当然不会在此过程中丢失信息。我必须在这里遗漏一些相当明显的东西。

我究竟做错了什么?

4

1 回答 1

3

对我来说完美无缺:(
我必须向 exception_impl 添加一个默认构造函数)

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

using std::cout;

class myException : public virtual boost::exception {

};

template <class T>
class exception_impl : public virtual std::exception, public T {
public:
    exception_impl() {}
    exception_impl(const T& ex) : T(ex) {}
    virtual const char* what() const throw() {return "blah";}
};

class some_exception : public myException {

};

struct tag_test_int;
typedef boost::error_info<tag_test_int,int> test_int_info;

void f()
{
    boost::throw_exception( exception_impl<some_exception>() << test_int_info(42) );
}

int main() {

    try {
        f();
    } catch (boost::exception& e) {
        cout << boost::diagnostic_information(e);
    }

    return 0;
}

输出:

Throw location unknown (consider using BOOST_THROW_EXCEPTION)
Dynamic exception type: N5boost16exception_detail10clone_implI14exception_implI14some_exceptionEEE
std::exception::what: blah
[P12tag_test_int] = 42

编译使用:

  • g++ (Ubuntu 4.9.2-0ubuntu1~14.04) 4.9.2
  • 提升 1.55

我认为问题在于您在构造函数内输出诊断信息,而 tag_test_int 未设置 jet。

于 2015-04-20T12:01:22.227 回答