Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all exceptions that could be thrown by a method

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?

like image 553
Kasper Holdum Avatar asked Apr 06 '10 12:04

Kasper Holdum


1 Answers

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 the language designers chose not to implement checked exceptions in C# (whether they outweigh the advantages or not is 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.

like image 140
Lucero Avatar answered Oct 20 '22 01:10

Lucero