如果投掷会发生什么;语句是在 catch 块之外执行的吗?

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

In C++ throw; when executed inside a catch block rethrows the currently caught exception outside the block.

在这个答案中,一个例外的想法当经常使用复杂的异常处理时,调度程序是作为减少代码重复的解决方案提出的:

In this answer an idea of exception dispatcher is brought up as a solution to reducing code duplication when using complex exception handling often:

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;
    }
}

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

Throwing a pointer or a value doesn't make any difference so it's out of the question.

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

What happens if ExceptionHandler() is called not from a catch block?

我用 VC7 试过这段代码:

I tried this code with VC7:

int main( int, char** )
{   
    try {
        throw;
    } catch( ... ) {
        MessageBox( 0, "", "", 0 );
    }
    return 0;
 }

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

First it causes the debugger to indicate a first-chance exception, then immediately an unhandled exception. If I run this code outside the debugger the program crashes the same way as if abort() has been called.

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

What is the expected behaviour for such situations?

推荐答案

From the Standard, 15.1/8

From the Standard, 15.1/8

如果当前没有处理异常,执行一个没有操作数的throw-expression调用std::terminate().

If no exception is presently being handled, executing a throw-expression with no operand calls std::terminate().

相关文章