In previous versions of Visual Studio, there was a single entry point to an extension (more specifically, an addin), the OnConnect
method. From there it was possible to perform all the Visual Studio operations, such as adding menu items, and setting up a global exception handler, to gracefully handle unhandled exceptions.
In Visual Studio 2010's VsPackage, things work differently - many parts of the application (such as IWpfTextView
) are composed by MEF and loaded without the explicit control of the package, making handling any exceptions coming from those very difficult.
Without explicitly wrapping all such components in a try..catch
of their own, how can I efficiently implement a global handler for (non-critical) exceptions in my VsPackage?
Again, the goal is having something like ReSharper's exception dialog, where it can handle global exceptions without crashing the Visual Studio.
Adding a Global Exception HandlerIn the Design tab part of the Ribbon, select New > Global Handler. The New Global Handler window opens. Type in a Name for the handler and save it in the project path. Click Create, a Global Exception Handler is added to the automation project.
An ExceptionFilterAttribute is used to collect unhandled exceptions. You can register it as a global filter, and it will function as a global exception handler. Another option is to use a custom middleware designed to do nothing but catch unhandled exceptions.
An exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero. Exceptions provide a way to transfer control from one part of a program to another. VB.Net exception handling is built upon four keywords - Try, Catch, Finally and Throw.
In Java, exception handling is done by try, catch blocks but spring boot also allows us to provide customized global exception handling where we need not to add try catch block everwhere, we can create a separate class for handling exceptions and it also separates the exception handling code from businesss logic code.
I have investigated this topic by reading official VsExtensibility forum and according to this and this threads there is no such one place to hook for catching all exceptions. So typical solution will be to use try... catches everywhere.
I suggest to use one of the AOP frameworks like PostSharp to accomplish this with attributes in order to eliminate tons of crappy duplicated code that's only purpose is to handle unhandled exceptions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With