C#: Does Visual Studio 2008 have a tool to show which Exceptions could be raised by a piece of code?

早过忘川 提交于 2019-12-05 08:57:25

问题


For example, if I'm opening a file, I know a FileNotFoundException might happen, or if I'm converting a String to double, a FormatException may happen. Obviously, if a method does both, both can be raised.

Is there a way to quickly see all possible exceptions raised by a method though? Keeping track of it myself seems error prone.


回答1:


It's not built into VS. There are 3rd party tools, though, like Redgate's exception hunter.

Edit I'm not employed by RG, but I am a fan of their products. I've tried this particular one, but we ended up not buying it.




回答2:


If memory serves me correctly if the intellisense tool tip should have a list of exceptions the method can throw. You can also open a browser tab in visual studio pointing to MSDN like so: http://msdn.microsoft.com/en-us/library/b9skfh7s.aspx#ddueExceptionsToggle




回答3:


The Agent Johnson pklugin to ReSharper




回答4:


I think Resharper does it if my memory serves correctly. Take a look at Pex, it might interest you too.




回答5:


There is no concrete way to find all exceptions a piece of code unless you have a way of running every possible branch in whatever piece of code you are exercising. While tools may be able to evaluate what are likely errors are to occur you still are going to run into situations which these tools will will not catch. While i'm not saying there is no reason to run them you still have to code is such a way to handle errors your tool may not catch. I have seen tools like this used in place of good testing and coding practices.

Also some times exceptions are good things. Some of the hardest and greatest impact errors I have ever found is due to developers handling errors in such a way that program continues but is no longer in a state that any future called code is expecting. just my .02



来源:https://stackoverflow.com/questions/2241070/c-does-visual-studio-2008-have-a-tool-to-show-which-exceptions-could-be-raised

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!