Logo
HOWTO: Handle exceptions in a Visual Studio add-in.

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: March 2010   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
Introduction

This article discusses some considerations when handling exceptions in a Visual Studio add-in.

More Information

When designing the exception handling mechanism of an add-in, the following important principles should be followed:

  • Since Visual Studio doesn't allow to use a global exception handler in an add-in (using the AppDomain.UnhandledException event), the following entry points of the add-in should include individual exception handlers (try/catch blocks):
    • Methods of the IDTExtensibility2 interface (OnConnection, OnStartupComplete, etc.)
    • Methods of the IDTCommandTarget interface (QueryStatus, Exec)
    • Event handlers of the EnvDTE automation interface (CommandEvents, WindowEvents, etc.)
    • Event handlers of controls in forms and usercontrols of the add-in
    • Etc.
  • All the exception handlers must call to a single global method that treats the exception (showing a message to the user, sending it back to the developer, etc.).
  • That single global method must receive the EnvDTE.DTE object. With that object, important information can be added to the bug report such as:
    • The IDE full name (DTE.FullName property).
    • The IDE version (DTE.Version property).
    • The IDE Edition (DTE.Edition property).
    • The IDE Locale ID (DTE.LocaleID property). This is important to troubleshoot problems that don't happen on the English Visual Studio of the developer of the add-in but can happen on the localized Visual Studio of other developers around the world using the add-in.
    • The IDE Registry Root (DTE.RegistryRoot property).
    • The IDE Display Mode (DTE.DisplayMode property).
    • The IDE Command Line Arguments (DTE.CommandLineArguments property).
  • Other important information (that doesn't require the DTE object) can be added to the bug report such as:
    • The .NET Framework version (System.Environment.Version property).
    • The add-in assembly file version (getting the AssemblyFileVersionAttribute of the assembly returned by System.Reflection.Assembly.GetExecutingAssembly()).
    • The add-in assembly location (System.Reflection.Assembly.GetExecutingAssembly().Location property).
  • You can add even more information about packages and service packs as described in the article HOWTO: Detect installed Visual Studio editions, packages or service packs.

Related articles


Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top