Search code examples
winapiseh

Windows Structured Exception Handling: simple test program will not compile


#include <windows.h>

int main()
{

    int* i = (int*)malloc(sizeof(int));
    *i = 5;

    __try
    {
        free(i);
        free(i);
    }
    __except
    {
        return -1;
    }


return 0;
}

I am trying to learn more about windows SEH. My first test program is giving me some real trouble. I have looked at the msdn documentation and I am still not really sure what I have wrong. I am getting the following errors when I try to compile this program:

error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'

both on line 15.

Thanks.


Solution

  • The problem is the __except clause needs to have an expression. See the following MSDN page for a complete sample

    http://msdn.microsoft.com/en-us/library/aa273608(VS.60).aspx

    Quick example which will always execute the handler

    __try {
      // stuff
    } __except (EXCEPTION_EXECUTE_HANDLER) {
      // handler
    }