none
Unhandled exceptions in event handler RRS feed

  • Question

  • I am using a third party COM component in a WPF application. I use the wrapper automatically generated by visual studio.

    When listening to events in the COM component, if an exception was thrown in the event handler it gets lost. The application doesn't crash and the Application's DispatcherUnhandledException handler is not fired.

    This behaviour was the natural behaviour in .NET framework 1.1, where the runtime (eats) those exceptions. I am using .NET framework 3.5.

    Any explanation for this behaviour?
    Thursday, April 23, 2009 11:51 AM

Answers

  • COM doesn't know beans about exceptions, it is not valid to let an exception pass out of a COM method.  Accordingly, the COM interop layer in the CLR installs an exception handler that translates .NET exception into an HRESULT.  There's a unique HRESULT for every .NET exception, check out the Exception.HResult property.

    The COM component will get that HRESULT back from invoking the event handler.  And, in the vast majority of cases, completely ignores it.  This horrid lossage is somewhat encouraged by COM development tools.  The wrapper generated by the ATL wizard for example doesn't check the return value.  Not much you can do about it, you'll get a cold shoulder from the COM developer.  Catch the exception yourself, log it and terminate the app.
    Hans Passant.
    Thursday, April 23, 2009 12:30 PM
    Moderator