Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a global exception handler for Visual Studio 2010 VsPackage

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.

like image 957
Igal Tabachnik Avatar asked Jun 12 '11 13:06

Igal Tabachnik


People also ask

What are the different ways to create a global exception handler?

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.

How exception is handled globally in C#?

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.

What is exception handling in Visual Studio?

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.

What is global exception handler in Java?

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.


1 Answers

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.

like image 92
Restuta Avatar answered Sep 28 '22 03:09

Restuta