Shaykatc's WebLog

VS 2003 Tip #8: Exception information in the Object browser

One of the complaints we received from VS7 was that you couldnt tell the exceptions that a particular method could throw. We scratched our heads thinking of ways to do this, as the xml docs did contain this informattion about the class libraries. Ideally we would have liked to have put this information into intellisense, but this request came up after we had done our coding for the milestone. The thing that pushed us off the edge was a usability test we saw, where a guy remarked how painful it was not to have that information.

We ended up adding this information to the object browser. To view it, try something like - Open Object Browser. Open System.IO.File and select the Open method in the right pane. At the bottom of the Object Browsers information pane (where they have method signatures and summary etc), scroll down and you'll see the Exceptions header which should list the exceptions the method can throw.

Addendum #1: This is C#/J# only.

Addendum#2: If you create a dll and put its xml comments file next to it and then view it through the Object browser in a C# project, you should see this for your exceptions too. The equivalent xml tags should be something of the sort:

<exception cref="T:System.ArgumentOutOfRangeException">

<paramref name="index" /> is less than zero. <para>-or-</para><para><paramref name="index" /> is greater than <see langword="Count" />.</para></exception>

 

Published Tuesday, March 16, 2004 2:17 PM by shaykatc

Comments

 

Sandspace said:

March 16, 2004 6:33 PM
 

Taco said:

Is there a way to add this info to the XML Documentation for your own class, and have it show up the same way?
March 16, 2004 3:44 PM
 

Hmmm... said:

I opened up my object browser and saw no exceptions for any of the File methods.
System.IO.File.Open, etc. I saw Summary, Parameters and Return Values. I am using VB.NET although I assume that should not make a difference.
March 16, 2004 4:51 PM
 

Nicholas Paldino [.NET/C# MVP] said:

Shay,

Here is the problem with that. Basically, the responsibility is left to me to create the XML documentation for what exceptions my method/property can throw.

Given the rich amount of metadata, as well as the IL, theoretically, you should be able to figure out all the exceptions that can be thrown by the method/property. I realize that this can be an arduous task (because you have to also analyze the IL to see if any are being caught, and not re-thrown) and would kill the Intellisense engine.

A way to get around this would be to create a lookup table for the methods/properties exposed by the assembly, and do the analysis at compile-time. Basically, this analysis would figure out what methods throw what exceptions, and then this can be looked up later in the code. I prefer this suggestion to Java's solution, which is to declare every exception the method can throw. That lead to most programmers just saying the method "throws Exception". Basically, if you create this lookup table, then doing the analysis in VS.NET for code not-yet-compiled would be less arduous (since you don't have to dig down a number of layers, you have all the information you need right there), and then should be able to be exposed through the Object browser without the need for XML documentation.

Hell, the XML documentation for exceptions could automatically be generated for you based on the metadata in your code. Try/catch blocks can be automatically generated for you in code, etc, etc. With that information available in metadata, a number of possibilities emerge.
March 17, 2004 5:22 AM
 

Impersonation Failure said:

I know me and David had this discussion a couple of times in the past, how to declare the possible exceptions that a function call might throw in c#. Speaking under correction but I think you can do it in C with exception specifications.
March 17, 2004 9:58 AM
 

Impersonation Failure said:

I know me and David had this discussion a couple of times in the past, how to declare the possible exceptions that a function call might throw in c#. Speaking under correction but I think you can do it in C with exception specifications.
March 17, 2004 10:10 AM
 

Omer van Kloeten said:

How about a feature in Whidbey using Smart Tags to allow you to decorate any calls with a try/catch clause for a specific exception or all of them?
March 17, 2004 9:09 AM
 

Shaykatc s WebLog VS 2003 Tip 8 Exception information in the Object | Paid Surveys said:

June 2, 2009 3:31 AM
 

Shaykatc s WebLog VS 2003 Tip 8 Exception information in the Object | Outdoor Decor said:

June 19, 2009 12:11 AM
New Comments to this post are disabled

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker