Office Watsoning and Exceptions in VSTO 2005 Beta 1

[EDITOR'S NOTE] Blogging has been slow as we are spending almost all our time getting Beta 2 out the door.

You may have had the experience of Excel or Word causing an Office Watson error at debug time when you have an exception that occurs in your Windows Forms event handlers in VSTO 2005.

The problem is related to the way that Windows Forms is integrated into Excel and Word. A Windows Forms windows procedure integrates with the existing Excel and Word message loops. When the debugger is attached, Windows Forms uses a windows procedure that doesn't catch any exceptions that occur so that you can see those exceptions in the debugger. When an exception occurs and you let it continue, the exception goes to the windows procedure and then propagates out to the Excel and Word message loops that are written in native code. The Excel or Word message loop sees an exception that it doesn't know how to handle and invokes Office Watson and shuts down the process.

Theoretically, this should also repro in managed add-in development in other applications like Outlook--for example, if you create a modeless form and throw an exception from a handler within the modeless form. I haven't gone and tried this though.

This doesn't occur when you have an exception in a COM event handler like say the BeforeDoubleClick event for a NamedRange. This is because the interop layer catches these exceptions and converts them to HRESULTs that Office ignores when firing events.

This also doesn't occur for Windows Forms event handlers when the debugger is not attached because Windows Forms uses a different windows procedure that catches exceptions that occur and displays a dialog box.

For VSTO 2005 Beta 2, we are working to change this behavior so Office won't Watson in any of these cases.

Also in Beta 1, you really should run with "Break when thrown" checked for Common Language Runtime Exceptions in your Exceptions dialog (Debug->Exceptions->Break On Specific Exceptions". Otherwise, you will miss exceptions that are occurring in your code because they'll often get caught by the interop layer.