c++ - 异常 - VC++2015 CTP Ultimate 中的不正确行为

标签 c++ exception

我有一个程序:

#include<iostream>
using namespace std;

class Test
{
public:
    void func()
    {
        cout << "Inside func" << endl;
        throw;
    }
};

int myfunc()
{
    Test T;
    T.func();
    return 1;
}

int main()
{
    myfunc();
    cout << "Main func" << endl;//should not print
    getchar();
}

我的预期是该程序将从 main 终止, 但在 VC++ 2015 上 main cout正在打印。这违反了我的理解,所以我用 gcc 编译了它在那里工作正常。

这是 VC++ 2015 中的错误还是像这样的程序终止行为是未指定/UB 行为?它是否应该执行 cout << "Main func" << endl;

IDE:VS2015 CTP Ultimate Preview(30天)

标志:/GS /analyze- /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"Debug\vc140.pdb" /fp:precise /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\exception.pch"

最佳答案

throw 没有参数,当在不正确的上下文中调用时,应该调用 terminate

根据标准:

A throw-expression with no operand rethrows the currently handled exception

....

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

然后行为取决于当前安装的 std::terminate_handler,但无论如何执行都应该终止。

Required behavior: A terminate_handler shall terminate execution of the program without returning to the caller.

Default behavior: The implementation’s default terminate_handler calls abort. The default implementation calls std::abort.

关于c++ - 异常 - VC++2015 CTP Ultimate 中的不正确行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31634829/

相关文章:

c++ - 为什么 cocos 2d-x v3 在 c++ 中如此频繁地使用指针?

c++ - 如何在没有得到 "lightweight-java-profiler"的情况下编译 "C++11 requires a space between literal and identifier"

java - 我们是否需要在使用 throws 声明的方法签名中已经提到的 catch block 中抛出相同的异常

java - 空对象引用上的 OnItemClick.onItemClick(int) 异常?

java - 为什么属性 '' 导致“异常”永远重复?

java - Android 中的 IllegalArgumentException

c++ - 使用|在 C++ 中进行声明时

c++ - 将对象传递给期望 shared_ptr 的函数而不实际共享所有权

c++ - 如何使用自定义消息扩展 Inet 的不同模块?

c# - 在 C# 中,如何序列化 System.Exception? (.Net CF 2.0)