26

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.

zxcvbnm
  • 1,833
  • 3
  • 27
  • 35
  • Seems like a dupe, http://stackoverflow.com/questions/1021104/unhandled-exception-checker-plugin-for-visual-studio http://stackoverflow.com/questions/232318/checked-exception-catching-in-c – Cory Charlton Feb 10 '10 at 23:29
  • 2
    In a sense this isn't possible (or at least isn't practical). Some exceptions depend on runtime conditions that can happen at any time, e.g. OutOfMemoryException. *Any* code could throw that. – Ryan Lundy Feb 11 '10 at 01:20

5 Answers5

10

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
  • 10,732
  • 5
  • 37
  • 66
1

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

Christopher Tarquini
  • 11,176
  • 16
  • 55
  • 73
  • 3
    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 Feb 10 '10 at 23:33
  • Would also mean to hover every single method call until IntelliSense pops up so you could check it out. – MasterMastic Feb 12 '14 at 10:23
1

The Agent Johnson pklugin to ReSharper

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0

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

Perpetualcoder
  • 13,501
  • 9
  • 64
  • 99
  • 1
    I have not encountered this functionality in Resharper myself (a regular user), but if it's there I'd love to know about it. – Redwood Feb 11 '10 at 01:53
0

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
  • 25,014
  • 6
  • 48
  • 78