SEH error reporting

1.3k views Asked by At

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?

2

There are 2 answers

3
mkaes On BEST ANSWER

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.

3
Stewart On

There are lots of things you need to be careful of when doing this. When your SEH handler runs, your process is in a completely unknown state and some of the things you try to do to instruct the user may fail. For example, if the exception was a stack overflow, there is very little you can do here. If you use a UI framework (MFC say) in here, it might be broken or in an inconsistent state because your app could have crashed mid way through some significant operation. If your app is multithreaded you need to know that those other threads will still be running when you get in to this filter, and handling that may take care.

If you really need to do this, one alternative approach is to have a watchdog process to do it.