[GIS] ArcGIS 10 add-in: Top-level exception handling

add-inarcgis-10.0arcobjectscnet

The ArcGIS 10 add-in I'm working on is pretty simple — just a tool control and a dockable window. I'm handling the specific exceptions I anticipate occuring at the source and throwing everything else, but what is the best practice for handling those unexpected exceptions in the add-in framework?

I'm currently just doing a catch (System.Exception ex) and showing it in a MessageBox in every method that doesn't have a higher-level method I could handle it in, but this doesn't seem like the best practice (and of course, FxCop is whining about it).

Is there any facility in the ArcGIS 10 add-in framework for a top-level exception handler to be hooked up, for example to the Application.ThreadException or AppDomain.UnhandledException events?

Seeing as add-ins are just class libraries and not applications with no access to the underlying application's startup code (from what I gather, those events have to be hooked up very early in the startup process), my guess is no, but I thought I'd ask if any experts out there had any suggestions on how "unexpected" exceptions should be handled in add-ins.

Best Answer

As far as I can tell you are implementing the error handling that ESRI is currently putting out there as best practice. If you were to grab a hold of the application's (ArcMap) unhandled exceptions you could potentially be then displaying messages about errors that were not part of your AddIn. Most of the AddIns you write are probably going to be buttons and those really only have two major routes that unexpected errors would be caught and displayed (onClick and onUpdate).

Just remember to use the 'throw' instead of 'throw ex'. There is a minute difference, but it results in retaining the error's lineage as it bubbles up from called functions.

Related Question