相关疑难解决方法(0)

抛出会发生什么; 语句是在catch块之外执行的?

在C++中,throw;当在catch块内执行时,重新抛出块外的当前捕获的异常.

这个答案中,当经常使用复杂的异常处理时,会提出异常调度程序的概念作为减少代码重复的解决方案:

try {
    CodeThatMightThrow();
} catch(...) {
    ExceptionHandler();
}

void ExceptionHandler()
{
    try {
        throw;
    } catch( FileException* e ) {
        //do handling with some complex logic
        delete e;
    } catch( GenericException* e ) {
        //do handling with other complex logic
        delete e;
    }
}
Run Code Online (Sandbox Code Playgroud)

抛出指针或值没有任何区别,所以这是不可能的.

如果不是从catch块调用ExceptionHandler()会发生什么?

我用VC7尝试了这段代码:

int main( int, char** )
{   
    try {
        throw;
    } catch( ... ) {
        MessageBox( 0, "", "", 0 );
    }
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

首先,它使调试器指示第一次机会异常,然后立即指示未处理的异常.如果我在调试器外运行此代码,程序崩溃的方式与调用abort()的方式相同.

这种情况的预期行为是什么?

c++ exception-handling exception visual-c++

12
推荐指数
1
解决办法
4743
查看次数

如果我使用"throw"会发生什么?无异常抛出?

这是设置.

我有一个C++程序调用几个函数,所有这些函数都可能抛出相同的异常集,并且我希望每个函数中的异常具有相同的行为(例如,打印错误消息并将所有数据重置为exceptionA的默认值;只需打印对于exceptionB;干净地关闭所有其他异常).

似乎我应该能够设置catch行为来调用私有函数,它只是重新抛出错误,并执行捕获,如下所示:

void aFunction()
{
    try{ /* do some stuff that might throw */ }
    catch(...){handle();}
}

void bFunction()
{
    try{ /* do some stuff that might throw */ }
    catch(...){handle();}
}

void handle()
{
    try{throw;}
    catch(anException)
    {
        // common code for both aFunction and bFunction
        // involving the exception they threw
    }
    catch(anotherException)
    {
        // common code for both aFunction and bFunction
        // involving the exception they threw
    }
    catch(...)
    {
        // common code for both aFunction and …
Run Code Online (Sandbox Code Playgroud)

c++ refactoring exception

9
推荐指数
2
解决办法
7372
查看次数