views:

268

answers:

4

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.

+10  A: 

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.

taylonr
Looks like a very promising tool. Why did you end up not buying it?
Eric J.
Can't remember exactly... we needed the profiler stat, and I think maybe budget was tight...
taylonr
+1  A: 

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

Chris T
Only if the person that wrote the method added the possible exceptions to the xml comments for that method: http://msdn.microsoft.com/en-us/magazine/cc302121.aspx
Neil Barnwell
A: 

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

Perpetualcoder
I have not encountered this functionality in Resharper myself (a regular user), but if it's there I'd love to know about it.
Lawrence Johnston
A: 

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

rerun