c++exceptionsehstructured-exception

SEH error reporting


I have a Visual Studio 2008 C++ program where the program is wrapped in a __try/__except block to capture any SEH exceptions. The exception filter creates an error log and gives the user detailed instructions on how to submit a defect report.

Does the code within the filter need to be wrapped in another __try/__except block? If not, what happens if it excepts? If so, how should that be handled?

static int MyFilter( struct _EXCEPTION_POINTERS* ep )
{
    /*
    Code to log the exception information, and instruct the user 
    on how to submit a defect report. Should this be in another
    __try/__except block?
    */
    return EXCEPTION_EXECUTE_HANDLER;
}

int WINAPI _tWinMain( HINSTANCE hInstance, 
                      HINSTANCE /*hPrevInstance*/, 
                      LPTSTR lpstrCmdLine, 
                      int nCmdShow )
{
    int result = 0;

    __try
    {
        result = Execute( hInstance, lpstrCmdLine, nCmdShow );
    }
    __except( MyFilter( GetExceptionInformation() ) )
    {
        // empty
    }

    return 0;
}

Thanks, PaulH


Edit: If MyFilter raises an exception, then I get in to an infinite exception loop. So, it looks like it does need __try/__except handling. I'm looking at doing this:

static int MyFilter( struct _EXCEPTION_POINTERS* ep )
{
    __try 
    {
        /*
        Code to log the exception information, and instruct the user 
        on how to submit a defect report. 
        */

       // cause an exception
       int x = 0, y = 1 / x;
    }
    __except( EXCEPTION_EXECUTE_HANDLER ) { /*empty*/ }
    return EXCEPTION_EXECUTE_HANDLER;
}

In this case, the program should have an abnormal termination and exception should be passed up to the OS for it to deal with. Is that correct?


Solution

  • If you raise an exception in your filter you will end up in the filter method again. Your exception will be handled by the same __except block.
    But there is no problem in using another __try __except block in your filter method.
    Unfortunately I cannot give you any references to this. I just tried it my self and you can too. Just force an division by zero.
    Usually I do not use SEH but the few times I did I had no issues in raising an exception in the filter method. But I did not find anything in the msdn when I looked for this.