开发者

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 开发者_JS百科runs through the call and check for any .NET exceptions being thrown?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜