非常简单的代码,并得到错误C2712,无法理解为什么

use*_*233 6 c++ visual-studio try-except

我有一段时间的麻烦error C2712: Cannot use __try in functions that require object unwinding,在缩小问题之后,我留下了一个非常非常简单的代码,我无法理解为什么它会导致这个错误.我在Windows下使用Visual Studio.

我用/ EHa编译(我不使用/ EHsc)

我使用__try/__except和不使用的原因try/catch是因为我想捕获所有错误,并且不希望程序在任何情况下崩溃,包括例如除以0,try-catch不会捕获.

#include <string>
static struct myStruct
{
    static std::string foo() {return "abc";}
};

int main ()
{
    myStruct::foo();

    __try 
    { }
    __except (true)
    { }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

error C2712: Cannot use __try in functions that require object unwinding
Run Code Online (Sandbox Code Playgroud)

Nay*_*iya 7

这是解决方案.有关更多详细信息,请阅读编译器错误C2712

#include <string>
struct myStruct
{
    static std::string foo() {return "abc";}
};

void koo()
{
    __try 
    { }
    __except (true)
    { }
}

int main ()
{
    myStruct::foo();   
    koo();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

额外注意:static如果没有使用你的struct(myStruct)声明,则不需要.