Exceptions Cheat Sheet

Exceptions Cheat Sheet

The most recent addition to my Cheat Sheets features details of all the Exceptions that can be found in the most commonly used assemblies within ASP.NET development. Compiling this information was an interesting challenge. I could have simply copy-pasted from MSDN, but that would have been extremely tedious. Instead, I ended up with a blend of Linq to XML, Reflection, a dash of Regex and the Global Assembly Cache Tool - gacutil.exe. Here's the full story.

Assemblies in the .NET Framework are discrete deployments of compiled code. Although Assemblies can contain more than one module, each of the .NET Framework assemblies contain just one. Among other things, each assembly contains Types. For the purposes of this exercise, I am only interested in types which are of type Exception. This is the base class from which all other Exceptions derive.
Locally, .NET Assemblies are stored in the Global Assembly Cache (GAC). However, I am not interested in obscure exception classes in assemblies that Web Forms developers won't be using, such as AudioException (found in System.Speech.dll). So I need a way to list all assemblies in the GAC and then select just those I am interested in. Second, I need to identify all objects of type Exception in them, and third, I need to generate some documentation that describes exactly what those exceptions mean, or what causes them for the cheat sheet to have any real value.
Gacutil.exe is a command line tool that comes as part of the .NET Framework SDK. One of the things this tool can do is to list all assemblies registered in the GAC. It will actually list them using their fully qualified name, such as System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e08. This is very useful, as it is this that will be needed later for when Reflection is used on the assembly.
The following code generates a List of AssemblyName objects. The Gacutil tool is located and fired up using a Process instance.  The Argument switch (a letter "l") tells the tool to simply list items in the GAC. The list is streamed as output which is then read line by line. If a line meets the criteria (it has a version, is mscorlib or begins with System) it is added as an Assembly obect to the collection. The reason for the filtering criteria is that my GAC is full to the brim with all sort of assemblies belonging to control sets I have installed among other things. I don't want a never-ending list.

Thanks Mike....

For more information : http://www.mikesdotnetting.com/Article/131/Using-gacutil.exe-and-Reflection-to-generate-the-Exceptions-Cheat-Sheet