6

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
user1438233
  • 1,153
  • 1
  • 14
  • 30
  • You are probably misguided in this desire to catch **all** exceptions. Anyway, to make this work, I think you need to move the `__try`/`__catch` blocks to a different function. – Praetorian Jul 15 '14 at 00:57
  • @Praetorian i did that, and it works, but all my main is full with static functions, and i want the try-except to be over all the main – user1438233 Jul 15 '14 at 01:00
  • What if you move everything within `main()` into another function (`do_main`), and then do `__try { do_main(); } ...`? – Praetorian Jul 15 '14 at 01:02
  • It might work, but what is the problem in this example? Why is it not working? – user1438233 Jul 15 '14 at 01:03
  • What is unclear about the error message exactly? Do you not understand why your function requires object unwinding? Do you not know that `__try` can't be used in a function that does? Are you looking for a workaround? (If so, we'll need to know more details about your specific use case.) – David Schwartz Jul 15 '14 at 01:04
  • Were I to stab a guess (and i'd probably be way off base but that rarely stops me) you can thank MS's extended lifetime of references to temporaries for this. – WhozCraig Jul 15 '14 at 01:05
  • 1
    The error message is pretty clear. The string object returned by `myStruct::foo()` requires stack unwinding for destruction, and catching SEH exceptions is not supported in such functions. – Praetorian Jul 15 '14 at 01:05
  • @Praetorian Isn't the destructor of `std::string` *supposed* to be long-gone before entering `__try`, or are the restrictions on setting up SEH function wide and not just no throw within the SEH construct? I rarely use them, and never in code like this, so I honestly don't know. – WhozCraig Jul 15 '14 at 01:08
  • @Praetorian I checked again, it will not work, I have to use a static struct before the `__try` and then during the `__except` to handle its data – user1438233 Jul 15 '14 at 01:08
  • @WhozCraig Yeah the string is destroyed at the end of that function call, but I think just having it in the same scope is not allowed. I don't know much about SEH, I've only used it once to debug a crash, and I moved everything within `main` to another function, and then wrapped that function call in a __try/__catch. – Praetorian Jul 15 '14 at 01:10
  • @user1438233 I don't understand why you have *anything* outside of the `__try`. Move *everything* within `main` into another function and call that function from within the `__try` block. – Praetorian Jul 15 '14 at 01:11
  • @Praetorian Say I have a data structure that I fill with data, if in the middle of the filling a have an error, I want in the `catch` to send the data I already have, so I must set the data structure out of the `__try` – user1438233 Jul 15 '14 at 01:21
  • add a language-tag for the version of compiler that you are using, this question is specific to that – M.M Jul 15 '14 at 01:26
  • @MattMcNabb This happens in VS2010 and VS2013, so i added VS tag – user1438233 Jul 15 '14 at 01:33
  • @WhozCraig The error message pretty clearly explains the (somewhat extreme) limitation, "Cannot use __try in **functions** that require object unwinding". – David Schwartz Jul 15 '14 at 01:38

1 Answers1

7

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).

Nayana Adassuriya
  • 23,596
  • 30
  • 104
  • 147