views:

110

answers:

2

I know that Java enforce the programmer to list all exceptions that will be thrown by the method, and thus creating an easy way of listing all possible thrown exception for the user of code.

.NET on the other hand has no such feature, and all we're left with is the API documentation or XML documentation where the exceptions are sometimes listed.

Is there any addons for VS that shows which exceptions any given call might throw? Given the power of reflection, shouldn't it be possible to look through the call, and look through all branches of possible runs through the call and check for any .NET exceptions being thrown?

+1  A: 

The only thing I have come across that does this is Exception Hunter, but it is a commercial tool that you would have to purchase.

adrianbanks
+7  A: 

The only tool I know of is the (commercial) Exception Hunter from red-gate software.

However, it's not really as deterministic as you might think first. Depending on the versions of additional assemblies used, the exceptions thrown may vary after build time if a newer version is used which does throw other exceptions than expected at build time.

In Java, you have the "special" RuntimeException which doesn't have to be declared in the method signature (including all exceptions descending from it). There are valid reasons why the language designers chose not to implement checked exceptions in C# (whether they outweight the advantages or not is however debatable); some Java devs just wrap exceptions in runtime exceptions, or they forget to use the "cause" exceptions which results in loss of information.

There is a good interview with Anders Hejlsberg about checked exceptions and some of the reasoning as to why C# doesn't have them - Thanks to adrianbanks for the link.

Lucero
There is a good interview with Anders Hejlsberg about checked exceptions and some of the reasoning as to why C# doesn't have them here: http://www.artima.com/intv/handcuffs.html
adrianbanks
@adrianbank, I know, but I just didn't have the link at hand. Thanks for posting it!
Lucero
Great article, gives a good perspective on checked exeptions for those of us who has never actually seem them in any practical use.
Qua