C++ 使用可能抛出c++;

C++ 使用可能抛出c++;,c++,exception,C++,Exception,我正在编写一个函数,该函数调用一个可能抛出的函数,以便初始化一个const成员。问题是:如果我把初始化放在try块中,当它超出范围时就会被删除。如果将初始化放在try块之外,则无法捕获异常。解决这一问题的办法是: double functionA() { double a; try { a=functionThatMayThrow(); } catch(std::except&p) { std::throw_with_nested(std::run

我正在编写一个函数,该函数调用一个可能抛出的函数,以便初始化一个const成员。问题是:如果我把初始化放在try块中,当它超出范围时就会被删除。如果将初始化放在try块之外,则无法捕获异常。解决这一问题的办法是:

double functionA()
{
  double a;
  try
  {
    a=functionThatMayThrow();
  }
  catch(std::except&p)
  {
    std::throw_with_nested(std::runtime_error(__func__);
  }
  const double b=a;
  //More code that uses b and computes x
  return x; 
}
我想知道是否有更好的方法来做到这一点。比如:

double functionA()
{
  const double b=functionThatMayThrow();
  //catch the exception somehow and rethrow with nested information

  //Unchanged
  //More code that uses b and computes x
  return x; 
}

也许我只是对这个问题采取了一种完全错误的方法。你能给我一些建议吗?任何与boost或c++11相关的解决方案都很好。

要向任何传递的异常添加一些跟踪信息,1可以执行以下操作:

auto func_A()
    -> double
{
    try
    {
        // The complete original function body.

        double const a = 2*some_func();
        return a/2;
    }
    catch( ... )
    {
        throw_with_nested( runtime_error( __func__ ) );
    }
}
但是,到处重复这一点可能会有点乏味,而且容易出错,因此您可能希望将逻辑集中化,而不是将函数表示为

auto func_A()
    -> double
{
    return with_trace( __func__, [&]() -> double
    {
        // The complete original function body.

        double const a = 2*some_func();
        return a/2;
    } );
}
现在,这需要一些支持机器,包括下行呼叫和上行呼叫,例如

#include <exception>    // std::throw_with_nested
#include <iostream>
#include <stdexcept>    // std::runtime_error, std::exception
#include <stdlib.h>     // EXIT_FAILURE, EXIT_SUCCESS
#include <string>       // std::string
#include <vector>       // std::vector

namespace cppx {
    using std::runtime_error;
    using std::throw_with_nested;

    template< class Func >
    auto with_trace( char const* const func_name, Func const body )
        -> decltype( body() )
    {
        try
        {
            return body();
        }
        catch( ... )
        {
            throw_with_nested( runtime_error( func_name ) );
        }
    }

    namespace x_io
    {
        using std::endl;
        using std::exception;
        using std::cerr;            // For client code.
        using std::ostream;
        using std::rethrow_if_nested;

        namespace impl {
            void print_on( ostream& stream, exception const& x, bool const is_nested )
            {
                stream << (is_nested? ">" : "!") << x.what() << endl;
                try
                {
                    rethrow_if_nested( x );
                }
                catch( exception const& nested_x )
                {
                    print_on( stream, nested_x, true );
                }
                catch( ... )
                {
                    stream << "<unknown exception type>" << endl;
                }
            }
        }  // namespace impl

        auto operator<<( ostream& stream, exception const& x )
            -> ostream&
        {
            impl::print_on( stream, x, false );
            return stream;
        }
    }    // namespace x_io
}  // namespace cppx

namespace my {
    using std::runtime_error;
    using std::string;
    using std::vector;
    using cppx::with_trace;

    bool fail_is_good;

    auto some_func()
        -> double
    {
        if( fail_is_good ) { throw runtime_error( "some_func" ); }
        return 3.14;
    }

    auto func_A()
        -> double
    {
        return with_trace( __func__, [&]() -> double
        {
            double const a = 2*some_func();
            return a/2;
        } );
    }

    void cpp_main( vector< string > const& args )
    {
        using std::cout; using std::endl;
        my::fail_is_good = (args.size() > 1);
        cout << func_A() << endl;
    }
}  // namespace my

auto main( int argc, char* argv[] )
    -> int
{
    using std::exception; using std::string; using std::vector;
    try
    {
        my::cpp_main( vector<string>( argv, argv + argc ) );
        return EXIT_SUCCESS;
    }
    catch( exception const& x )
    {
        using namespace cppx::x_io;
        cerr << x << endl;
    }
    return EXIT_FAILURE;
}
\include//std::throw\u与\u嵌套
#包括
#包括//标准::运行时错误,标准::异常
#包括//退出失败,退出成功
#include//std::string
#include//std::vector
命名空间cppx{
使用std::runtime\u错误;
使用std::throw_和_嵌套;
模板
自动跟踪(字符常量*常量函数名,函数常量正文)
->decltype(body())
{
尝试
{
返回体();
}
捕获(…)
{
用嵌套的方式抛出(运行时错误(函数名));
}
}
名称空间x_io
{
使用std::endl;
使用std::exception;
使用std::cerr;//作为客户端代码。
使用std::ostream;
如果嵌套,则使用std::rethrow\u;
名称空间impl{
无效打印(ostream和stream、异常常量和x、布尔常量嵌套)
{
流1);

您是否可以
尝试{const double b=maytrow();//更多使用b并计算x的代码;返回x;}catch(std::exception&p)…
,但是为什么
b
必须
const
?您不能费心在函数中捕获异常,而让外部的
catch()执行
block处理它。毕竟,如果变量没有被设置,函数还能继续吗?你想通过将函数名添加到任何传递的异常中来构建堆栈跟踪,是吗?@cicto:我明白了。我发现你的解决方案很好(我现在感觉很笨)。const b要求主要是为了const正确性。@KahnYe:假设您有足够的理由这样做,一个简单直接的方法是将所有函数体放在
try
子句中。异常起源于何处并不重要,如果它从
try
子句中逃逸,则您可以
捕获它并添加函数您甚至可以将该逻辑集中到以lambda和函数名为参数的函数中。