【问题标题】:Adding error_info to std::exception将 error_info 添加到 std::exception
【发布时间】:2014-10-21 02:16:47
【问题描述】:

我正在将boost::exception 集成到现有代码中。有些代码现在使用BOOST_THROW_EXCEPTION,但有些可能仍会抛出标准的std::exception

我想在中间捕获站点添加 error_info。根据文档,如果异常是boost::exception,我可以这样做:

try {
    do_something()
}
catch( boost::exception & e ) {
    e << boost::errinfo_file_name(file_name);
    throw;
}

但这只会将信息添加到提升异常中。我也想将它添加到std::exception。最干净的方法是什么?这是一种方法,但会导致一些代码重复:

try {
    do_something()
}
catch( boost::exception & e ) {
    e << boost::errinfo_file_name(file_name);
    throw;
}
catch( std::exception & e ) {
    throw enable_error_info(e) << boost::errinfo_file_name(file_name);
}

是否有一种方法相当于“将当前异常作为 boost 异常给我,或者如果它不是一个则创建一个 boost 异常”?

编辑boost::enable_error_info() 有点这样做,但会返回原始异常的副本,它会切掉我正在捕获的异常的 boost::exception 部分。举个例子:

int main()
{
    try {
        try {
            BOOST_THROW_EXCEPTION( std::runtime_error( "foo" ) );
        }
        catch( std::exception & e ) {
            std::cerr << e.what() << std::endl; // "foo" 
            if( const char* const* function = boost::get_error_info<boost::throw_function>(e) ) std::cerr << *function << std::endl; // "int main()"
            throw boost::enable_error_info(e) << boost::errinfo_file_name("bar");
        }
    }
    catch( std::exception & e ) {
        std::cerr << e.what() << std::endl; // "std::exception" 
        if( const char* const* function = boost::get_error_info<boost::throw_function>(e) ) std::cerr << *function << std::endl; // NOTHING
    }

    return 0;
}

编辑:我尝试使用boost::current_exception(),它也将事情切掉了。基本上,由于多重继承导致的切片,任何复制异常的尝试都会丢失一些数据。与文档说您应该始终使用 throw 而不是 throw e 重新抛出的原因相同。所以我真的不想招致任何复制,除非有必要。

理想情况下,我想写以下内容,如果current_exception_as_boost_exception() 已经是boost::exception,则返回对当前异常的引用,否则返回对其调用boost::enable_error_info 的结果。

try {
    do_something()
}
catch( std::exception & e ) {
    throw current_exception_as_boost_exception() << boost::errinfo_file_name(file_name);
}

这就是boost::enable_current_exception 的用途吗?真的不清楚它的用途是什么,并且没有在任何教程中使用。

【问题讨论】:

  • 您似乎说enable_error_info() 有效,但“矫枉过正”。谁在乎它是不是矫枉过正,因为通常不应该发生异常,所以性能并不是一个真正的问题?
  • 我不太关心性能,而是担心在复制过程中丢失一些信息的可能性。 boost::copy_exception()(我假设这是enable_error_info() 使用的)可能会在过程中丢失类型信息(请参阅stackoverflow.com/questions/9973499/…)。所以我宁愿避免复制,除非我能保证enable_error_info() 不会丢弃任何东西。

标签: c++ exception boost


【解决方案1】:

这是一个可以满足我要求的解决方案。但如果我觉得我在这里重新发明了一些东西。难道没有内置的方法来实现同样的事情吗?

struct rethrow
{
    rethrow()
    {
        try{ throw; }
        // Already a boost::exception
        catch( boost::exception& ) {} 
        // Something else. Make it a boost::exception
        catch( ... ) { ptr = boost::current_exception(); } 
    }

    template<class T> 
    rethrow const& operator<<( const T& t ) const
    {
        try
        {
            re();
        }
        catch( boost::exception& e )
        {
            e << t;
        }
        return *this;
    }

    ~rethrow()
    {
        re();
    }

private:
    void re() const
    {
        if( !ptr ) throw;
        else boost::rethrow_exception( ptr );
    }

    boost::exception_ptr ptr; 
};


int main()
{
    try {
        try {
            throw std::runtime_error( "foo" ); // or BOOST_THROW_EXCEPTION( std::runtime_error( "foo" ) );
        }
        catch( std::exception & e ) {
            rethrow() << boost::errinfo_file_name("bar");
        }
    }
    catch( std::exception & e ) {
        std::cerr << __LINE__ << ": caught " << e.what() << std::endl; // "caught foo"
        if( const char* const* function = boost::get_error_info<boost::throw_function>(e) ) std::cerr << __LINE__ << ": throw from " << *function << std::endl; // "throw from int main()" (when using BOOST_THROW_EXCEPTION)
        if( std::string const* fileName = boost::get_error_info<boost::errinfo_file_name>(e) ) std::cerr << __LINE__ << ": trying to open " << *fileName << std::endl; // "trying to open bar"
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 2018-06-19
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2011-07-16
    • 2014-11-03
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多