tags:

views:

589

answers:

4

Place this code somewhere in a Word document level VSTO solution outside of ThisDocument_Startup (create a ribbon button, with a click event):

int zero = 0;
int divideByZero = 10 / zero;

Start without debugging (Ctrl + F5), Result: Exception is swallowed, the rest of the code fails silently.

The exception will be visible if placed in ThisDocument_Startup, but it appears nowhere else. Microsoft's VSTO forums and the MSDN documentation seem to feel Try...Catch should be used - which is not a big deal for known unknowns. What about the unknown unknowns?

All the common ways of dealing with unhandled exceptions for managed code don't seem to work, presumably because of VSTO using managed code with Office COM Interops:

//These don't work
AppDomain.CurrentDomain.UnhandledException ...
System.Windows.Forms.Application.ThreadException ...

I've read posts about troubleshooting VSTO code that always seems to lead to placing Try...Catch around practically everything!

Is there any better way to handle known and unknown (now invisible and silent!) failures?

+1  A: 

Office tends to eat every exception. I would always log everything to a file. Don't expect any help from MS.

eschneider
Also there is an option in Office "Report addin errors to user" but don't expect it to work, I know for a fact it does not.
eschneider
Event logs for Office and windows are always empty. They really do even try to help the developers.
eschneider
A: 

i can get a ********** Exception Text ********** System.DivideByZeroException: Attempted to divide by zero.

with app level addin project.

Are you only seeing this with Doc level customisation projects? Also only in word or is this also happening in Excel?

I apologize for my imprecision - yes, only for Doc level projects in Word.
Mike Regan
A: 

I think the problem is isolated only to exceptions around the "add-in user interface" - which happens to be a direct setting found here (Word 2007):

Word Options > Advanced > General > "Show add-in user interface errors"

Mike Regan
Do you know if there is an equivalent option for Word 2003? I'm struggling to get proper exception handling working in an MS Word Application level VSTO add-in. I've added the exception handlers as per your question but they never trigger...
Ben Robbins
I don't know of one in 2003. For our VSTO applications in Office 2007, we've had to carefully try...catch around everything with logging and safe tear-down for unknown exceptions.
Mike Regan
Thanks Mike. I notice your question was about doc level add-ins. Have you done any app level add-ins and did you manage to get global error handlers working for VSTO application level add-ins, or did you have to put try/catch around everything for them too?
Ben Robbins
Yes, similar unfortunate experience with app-level add-ins. I end up placing try..catch at the lowest level of the call stack (which still ends being everywhere) for dealing with exceptions.
Mike Regan
A: 

I had the same issue with an application-level add-in in Word.

If you are running your add-in by launching it from Visual Studio with F5, then you'll always get "unhandled by user code" in VS (eg 2008 SP1), unless:

  • you uncheck the Visual Studio setting "Break when an exception is .. user-unhandled" for CLR exceptions, or

  • you liberally apply the [System.Diagnostics.DebuggerNonUserCodeAttribute()] annotation.

Once you've done one of these, UnhandledException/ThreadException seem to work.

plutext