C++ 是否有方法确定是否发生异常?

C++ 是否有方法确定是否发生异常?,c++,exception,exception-handling,destructor,C++,Exception,Exception Handling,Destructor,在析构函数中,是否有方法确定当前是否正在处理异常?您可以使用std::uncaught_exception(),但它可能不会执行您认为它可以执行的操作:有关详细信息,请参阅。您可以使用std::uncaught_exception(),但它可能不会像您认为的那样:有关更多信息,请参阅。您可以使用。下面是一个小例子: struct my_exception1 { explicit my_exception1( int res_code ) : m_res_code( res_code

在析构函数中,是否有方法确定当前是否正在处理异常?

您可以使用std::uncaught_exception(),但它可能不会执行您认为它可以执行的操作:有关详细信息,请参阅。

您可以使用std::uncaught_exception(),但它可能不会像您认为的那样:有关更多信息,请参阅。

您可以使用。下面是一个小例子:

struct my_exception1
{
    explicit    my_exception1( int res_code ) : m_res_code( res_code ) {}
    int         m_res_code;
};


struct my_exception2
{
    explicit    my_exception2( int res_code ) : m_res_code( res_code ) {}
    int         m_res_code;
};

class dangerous_call {
public:
    dangerous_call( int argc ) : m_argc( argc ) {}
    int operator()()
    {
        if( m_argc < 2 )
            throw my_exception1( 23 );
        if( m_argc > 3 )
            throw my_exception2( 45 );
        else if( m_argc > 2 )
            throw "too many args";

        return 1;
    }

private:
    int     m_argc;
};


void translate_my_exception1( my_exception1 const& ex )
{
    std::cout << "Caught my_exception1(" << ex.m_res_code << ")"<< std::endl;
}


void translate_my_exception2( my_exception2 const& ex )
{
    std::cout << "Caught my_exception2(" << ex.m_res_code << ")"<< std::endl;
}



int 
cpp_main( int argc , char *[] )
{ 
    ::boost::execution_monitor ex_mon;
    ex_mon.register_exception_translator<my_exception1>(
        &translate_my_exception1);
    ex_mon.register_exception_translator<my_exception2>(
        &translate_my_exception2);
    try{
     // ex_mon.detect_memory_leak( true);
      ex_mon.execute( ::boost::unit_test::callback0<int>( 
          dangerous_call( argc ) ) );
    }   
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }
    return 0;
}
struct my\u例外1
{
显式my_异常1(int resu代码):m_resu代码(resu代码){}
int m_res_代码;
};
结构我的例外2
{
显式my_exception2(int Resu代码):m_Resu代码(Resu代码){}
int m_res_代码;
};
班级危险呼叫{
公众:
危险呼叫(int argc):m_argc(argc){
int运算符()()
{
如果(m_argc<2)
抛出我的例外1(23);
如果(m_argc>3)
抛出我的例外2(45);
否则,如果(m_argc>2)
抛出“太多args”;
返回1;
}
私人:
int m_argc;
};
无效翻译我的例外1(我的例外1常量和ex)
{
std::cout您可以使用。请看这里的一个小示例:

struct my_exception1
{
    explicit    my_exception1( int res_code ) : m_res_code( res_code ) {}
    int         m_res_code;
};


struct my_exception2
{
    explicit    my_exception2( int res_code ) : m_res_code( res_code ) {}
    int         m_res_code;
};

class dangerous_call {
public:
    dangerous_call( int argc ) : m_argc( argc ) {}
    int operator()()
    {
        if( m_argc < 2 )
            throw my_exception1( 23 );
        if( m_argc > 3 )
            throw my_exception2( 45 );
        else if( m_argc > 2 )
            throw "too many args";

        return 1;
    }

private:
    int     m_argc;
};


void translate_my_exception1( my_exception1 const& ex )
{
    std::cout << "Caught my_exception1(" << ex.m_res_code << ")"<< std::endl;
}


void translate_my_exception2( my_exception2 const& ex )
{
    std::cout << "Caught my_exception2(" << ex.m_res_code << ")"<< std::endl;
}



int 
cpp_main( int argc , char *[] )
{ 
    ::boost::execution_monitor ex_mon;
    ex_mon.register_exception_translator<my_exception1>(
        &translate_my_exception1);
    ex_mon.register_exception_translator<my_exception2>(
        &translate_my_exception2);
    try{
     // ex_mon.detect_memory_leak( true);
      ex_mon.execute( ::boost::unit_test::callback0<int>( 
          dangerous_call( argc ) ) );
    }   
    catch ( boost::execution_exception const& ex ) {
        std::cout << "Caught exception: " << ex.what() << std::endl;
    }
    return 0;
}
struct my\u例外1
{
显式my_异常1(int resu代码):m_resu代码(resu代码){}
int m_res_代码;
};
结构我的例外2
{
显式my_exception2(int Resu代码):m_Resu代码(Resu代码){}
int m_res_代码;
};
班级危险呼叫{
公众:
危险呼叫(int argc):m_argc(argc){
int运算符()()
{
如果(m_argc<2)
抛出我的例外1(23);
如果(m_argc>3)
抛出我的例外2(45);
否则,如果(m_argc>2)
抛出“太多args”;
返回1;
}
私人:
int m_argc;
};
无效翻译我的例外1(我的例外1常量和ex)
{

std::cout正如Luc所说,您可以使用std::uncaught_exception()。但是为什么您想知道呢?在任何情况下,!

正如Luc所说,您可以使用std::uncaught_exception()。但是为什么您想知道呢?在任何情况下,!

当然,您的测试函数可以位于一个结构内部,在该结构中您可以重载()运算符…您可以在那里放置测试以查看生成了什么异常建议:如果您使用“代码示例”格式(答案编辑器中的“1”和“0”按钮)来包装代码示例,那么您的代码示例将更易于阅读。当然,您的测试函数可以位于一个结构的内部,您可以在其中重载()运算符…您可以将测试放在那里查看生成了什么异常建议:如果使用“代码示例”格式(答案编辑器中的“1”和“0”按钮)来包装代码示例,您的代码示例将更易于阅读是的,有一种试图避免以终止()结束的味道在已经展开时不抛出。这听起来很可疑。如果发生异常,为什么要进行不同类型的清理?同意可疑。并且您永远不想抛出析构函数。但是您可以从析构函数抛出,但是如果在另一个异常传播时从析构函数抛出,应用程序将终止ate(不退出或正常清理)这听起来很可疑。如果发生异常,为什么要进行不同类型的清理?同意,这是可疑的。而且您永远不想从析构函数中抛出。但是您可以从析构函数中抛出,但是如果在另一个异常传播时从析构函数抛出,则应用程序将终止(不退出或正常清理)