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

I am having problems with help error C2712: Cannot use __try in functions that require object unwinding, after narrowing the problem I still have very simple code, and I can’t understand why it is causing this error. I am using Visual Studio under windows.

I compile with / EHa (I do not use / EHsc)

The reason I use __try/__except, and not try/catch, is that I want to catch ALL errors and do not want the program to crash under any circumstances, including, for example, dividing by 0, this try-catch is not a 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
+4
source share
1 answer

Here is the solution. Read C2712 Compiler Error for more details .

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

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

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

: static, (myStruct).

+5

Source: https://habr.com/ru/post/1548424/


All Articles