如果投掷会发生什么?语句在catch块之外执行? [英] What happens if a throw; statement is executed outside of catch block?

查看:124
本文介绍了如果投掷会发生什么?语句在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.

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

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?

推荐答案

从标准中,15.1 / 8

From the Standard, 15.1/8


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

这篇关于如果投掷会发生什么?语句在catch块之外执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆