You are currently offline, waiting for your internet to reconnect

BUG: Unhandled exception filter not called inside debugger

This article was previously published under Q173652
Win32 processes can install an unhandled exception filter function to catchexceptions that are not handled in a __try/__except block on a process-widebasis. When debugging such a process, you find that the unhandled exceptionfilter is never executed, even when you put a breakpoint inside it.However, when the process is not being debugged, its unhandled exceptionfilter is called as expected.
When called from a process being debugged, the UnhandledExceptionFilter()Win32 API function does not call the application-installed unhandledexception filter.

Note The UnhandledExceptionFilter() API determines whether the process is being debugged. If the process is being debugged, the UnhandledExceptionFilter API passes the exception to the debugger. Then, the UnhandledExceptionFilter API calls the unhandled exception filter for the process.
UnhandledExceptionFilter should call the application-installed unhandledexception filter for processes that are being debugged in addition to thosenot being debugged.

One way to debug an unhandled exception filter function is to put a__try/__except block around all of the code inside the main() or WinMain()function as follows:
   void main (int argc, char **argv)   {      __try      {      // all of code normally inside of main or WinMain here...      }      __except (MyUnFilter (GetExceptionInformation()))      {         OutputDebugString ("executed filter function\n");      }   }				
This work around behaves slightly differently from an unhandled exceptionfilter in that it will only be called for the thread executing the main()or WinMain() function. If you are debugging a multithreaded application,then you will need to add a similar __try/__except block inside of eachthread function.

If you don't want to add new __try/__except blocks to debug an unhandledexception filter, you should use other methods such as assert, traceoutputs, and debugging code to validate the behavior of the exceptionhandler.
Microsoft has confirmed this to be a bug in the Microsoft products listedat the beginning of this article. We are researching this bug and will postnew information here in the Microsoft Knowledge Base as it becomesavailable.
The purpose of an unhandled exception filter function is to give a processthe opportunity to respond to exceptions that occur outside any__try/__except block. Unhandled exception filters are process-wide; if anythread causes an exception, but does not handle it, the unhandled exceptionfilter will get called. Once called, the filter function can fix the causeof the exception and continue execution, or it can pass the error to thedefault unhandled exception handler, which terminates the process.

Win32 applications can install a process-wide unhandled exception filterwith the SetUnhandledExceptionFilter() API function. The Microsoft VisualC++ C Runtime (CRT) library startup code implements a __try/__except blockaround the main() function. When an exception is caught by this block, itsexception filter function, named _XcptFilter, is called. _XcptFilter doestwo things: it maps the Win32 exceptions to signals used by the signal()CRT function, and dispatches signals to their appropriate handlers. If asignal's handler is set to SIG_DFL (as is the case by default), _XcptFiltercalls the UnhandledExceptionFilter() API.

The UnhandledExceptionFilter() API determines whether the process is beingdebugged. If it is, then UnhandledExceptionFilter passes the exceptionfirst to the debugger, and then is supposed to call the process's unhandledexception filter.

If the process isn't being debugged, then UnhandledExceptionFilter callsthe process's unhandled exception filter. Depending on the filterfunction's return, UnhandledExceptionFilter either continues execution atthe point of the exception, or displays a system-modal dialog box informingthe user that the application is to be terminated due to an unhandledexception.

Steps to Reproduce Behavior

Compile the following sample code, and then set a breakpoint insideMyUnFilter. Observe that the breakpoint never gets hit.
   /*      Demonstrates how to use SEH so that unhandled exception filter      can trap unhandled instructions.   */    #include <windows.h>   #include <stdio.h>   LONG WINAPI MyUnFilter (struct _EXCEPTION_POINTERS *lpExceptionInfo);   void main (void)   {      LPBYTE lpBuff = NULL;      SetUnhandledExceptionFilter (MyUnFilter);      *lpBuff = 54;   }   LONG WINAPI MyUnFilter (struct _EXCEPTION_POINTERS *lpExceptionInfo)   {      static int s_SigCount = 0;      DWORD dwExceptCode = lpExceptionInfo -> ExceptionRecord ->                           ExceptionCode;      LONG  lResult;      if (dwExceptCode == EXCEPTION_ACCESS_VIOLATION)      {         printf ("access violation caught by MyUnFilter\n");         ++s_SigCount;         // test case to keep this from going on forever (since we're not         // fixing the AV)            if (s_SigCount < 5)               lResult = EXCEPTION_CONTINUE_EXECUTION;            else               lResult = EXCEPTION_EXECUTE_HANDLER;      }      else         lResult = EXCEPTION_CONTINUE_SEARCH;      return lResult;   }				

Article ID: 173652 - Last Review: 11/21/2006 15:44:03 - Revision: 4.4

Microsoft Win32 Application Programming Interface

  • kbbug kbexcepthandling kbkernbase kbpending KB173652