Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
by Mathias 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.

More...

Comments

Comment RSS