very simple code, and getting error C2712, could not understand why

℡╲_俬逩灬. 提交于 2019-12-21 05:07:28

问题


I'm having trouble for a while with error C2712: Cannot use __try in functions that require object unwinding, after narrowing the problem, I was left with a very very simple code, and i can not understand why it causes this error. I am using Visual Studio under windows.

I am compiling with /EHa (I do not use /EHsc)

The reason I use __try/__except and not try/catch is because I want to catch ALL the errors, and do not want the program to crash under any circumstances, including for example division by 0, that try-catch does not catch.

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

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

    __try 
    { }
    __except (true)
    { }

    return 0;
}

output:

error C2712: Cannot use __try in functions that require object unwinding

回答1:


Here is the solution. For more details read Compiler Error C2712

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

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

int main ()
{
    myStruct::foo();   
    koo();
    return 0;
}

Extra Note: no need static if no declaration using your struct (myStruct).



来源:https://stackoverflow.com/questions/24748384/very-simple-code-and-getting-error-c2712-could-not-understand-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!