Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
30. May 2010 11:01

Silence is gold. Or… is it? You may have noticed that VSTO swallows exceptions; that is, if something goes wrong in your add-in code, Office will discreetly carry on as if nothing had happened. Consider the following code:

public partial class ThisAddIn
{
private int counter;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.SheetActivate += SheetActivated;
}

private void SheetActivated(object sheet)
{
MessageBox.Show("Counter = " + this.counter.ToString());
throw new ArgumentException("Something went south here.");
counter++;
}

The add-in is supposed to maintain a counter of how many times the user has changed the activate sheet. However, a bug throws an exception right before the counter is updated. If you run this code, you’ll see that the MessageBox keeps being displayed every time you change the selected worksheet, but the counter stays firmly at zero, and never gets updated.

On the plus side (?), this spares your user the scary-looking notification a traditional Winforms application puts in your face when an unhandled exception is thrown:

On the down side, it means that you’d better think about a strategy to diagnose your add-in if something goes wrong: if you don’t put in place some exception logging or feedback mechanism, you won’t have a clue what is going wrong, or even that something is going wrong.

Now if you are using Visual Studio 2008 (and not the non-existent 2007 as I initially wrote), you want to be extra-careful: in debug mode, by default your VSTO project will behave the same way, and happily go on without notifying you of unhandled exceptions. If you want to go on a serious bug hunt, one thing you can do is turn on the Ripley mode: go to Debug > Exceptions (or Ctrl + Alt + E), and select Common Language Runtime Exceptions / Thrown.

Visual Studio will now immediately highlight the guilty code whenever a .NET exception is thrown.

Note that I said Visual Studio 2008, and not simply Visual Studio. When I tried the same in Visual Studio 2010, I was very pleasantly surprised: the exception was immediately highlighted, without having to do anything. Bugs, beware: Ripley mode is on out-of-the box in Visual Studio 2010!

Comments

6/2/2010 4:26:40 PM #

Very nice tip, Mathias, but I think you mean Visual Studio 2008.  Or are you using a special VS version that only VIPS can get?

6/2/2010 4:43:37 PM #

Good catch, Paul - thank you! Indeed, I am not using Visual Studio 2007, or 2008 1/2, or any other Secret Version, but plain old Visual Studio 2008. Hopefully, someone will create one day the magic "Break on silly Blogging Exceptions" checkbox, in the meanwhile, I'll have to count on the vigilance of the readers
Mathias

Add comment

• Comment
• Preview