23

I work on a quite large legacy codebase. Because it's not always been maintained the best, sometimes exceptions are used from control flow, or for varying other reasons. And there are times where it is nearly unavoidable For instance, how else do you check if a file is a valid .ico image other than pass it in as an image and see if it doesn't throw an exception?

I refactor this kind of stuff where I can, but many times it's just too expensive to refactor for little gain. These bogus exceptions become extremely annoying when debugging. We catch all exceptions to avoid our program ever crashing, and catch most exceptions and display something a bit more user friendly. So, when debugging, if some piece of code throws an ApplicationException, there might be 50 exceptions of that type thrown before we finally get to the actual bug. Most of the time these bogus exceptions are focused around a single part of code(many times a single line). Is there any way I could get Visual Studio to ignore exceptions thrown from that line, but still stop on the exception that is the actual problem? Or is there anything else I can do to help prevent this kind of debugging frustration?

To illustrate my problem, imagine something like this:

for(int i=0; i<foo; i++)
{
  try
  {
    FooBar(i); //this function throws NullReferenceException sometimes
  }catch {} //ignore it because we don't care if it failed
}
....
var tmp=Bar as FooType; //this cast fails so tmp is null
tmp.Meh(); //throws exception here. This is a bug, we should've checked for null

If you wanted to figure out where the NullReference is, you basically hold down F5 until you're past the FooBar calls. This is annoying at best, and quite error prone

David Brabant
  • 41,623
  • 16
  • 83
  • 111
Earlz
  • 62,085
  • 98
  • 303
  • 499
  • Ever try [ReSharper](http://www.jetbrains.com/resharper)? It won't address all the problems you're having but it makes refactoring quickly a whole lot easier. – Darth Continent Jan 08 '13 at 16:27
  • @DarthContinent have it. Still doesn't help with this kind of thing though – Earlz Jan 08 '13 at 16:27
  • 2
    Heh, based on the lack of response, I may have to dive in and make a Visual Studio add-on – Earlz Jan 08 '13 at 17:08
  • Also see this question http://stackoverflow.com/questions/948590/suppress-first-chance-exceptions – Chris Chilvers Jan 09 '13 at 12:54
  • You might try using a tool like ReSharper to find (and in some cases remove) these code smells. – John Saunders Jan 10 '13 at 06:07
  • Possible duplicate of [How to force visual studio debugger to skip specific exceptions?](https://stackoverflow.com/questions/7518080/how-to-force-visual-studio-debugger-to-skip-specific-exceptions) – Liam Jan 02 '19 at 10:35

3 Answers3

1

You can tune on what exceptions types the break occurs. See Debug -> Exceptions in menu http://msdn.microsoft.com/en-us/library/d14azbfh(v=vs.80).aspx

Elastep
  • 3,272
  • 1
  • 14
  • 16
  • 3
    See my example though. I can either ignore NullReference exceptions, or miss the bug, or I can continue through the bogus exceptions. This isn't fine grained enough – Earlz Jan 08 '13 at 17:01
  • You can use Debugger.Break() to break on the exception you need manually. – Elastep Jan 08 '13 at 21:15
  • 4
    Well, that's the problem. I usually don't know where exactly the exception is that I need to pay attention to, I just know that I don't need to pay attention to these exceptions which are thrown and then immediately caught and ignored – Earlz Jan 08 '13 at 21:26
1

From what I see you can combine multiple techniques of debugging to improve your refactoring process.

If possible you can place (parts of) the legacy code into seperate assemblies. Compile those with optimization and enable "Just my Code" debugging.

For smaller blocks (Methods) you can use the DebuggerStepThrough Attribute, so the debbuger won't break there. For your example you could create a method that permorms the loop which calls your FooBar Method and place [DebuggerStepThrough] on the newly created method. Alternativly you could refactor FooBar to handle the exception and place [DebuggerStepThrough] on that.

Jaster
  • 8,255
  • 3
  • 34
  • 60
  • 1
    This doesn't really work for all cases. In one case the exception is thrown, and handeled 5 calls up (perfect example of exceptions as control flow). However, by using `[DebuggerHidden]` I can at get the debugger to ignore some of the exceptions that are handled within one method – Earlz Jan 09 '13 at 17:35
  • Note that while the option for Just My Code is also available for C++, it doesn't have an effect on exceptions, as described here: http://msdn.microsoft.com/en-us/library/dn457346.aspx – pjcard Jan 31 '14 at 15:31
0

I develop IntelliDebugger - extension for Visual Studio, which facilitates debugging C++/C# code. Including work with exceptions. IntelliDebugger can filter exceptions that have been thrown out of modules are not included in the Solution (this feature called ”Break exceptions only from Solution”). Perhaps this feature will be useful to you.

In future versions we are planning to add a better filter and other features to work with exceptions. If you have any feature requests or bug reports, please write to me. This will help us make the product handy specifically for you.

Mikhail Shcherbakov
  • 1,826
  • 16
  • 22
  • This sounds rather close to fixing the problem, but it doesn't cover all of my cases. I'm actually using this problem as an excuse to try my hand at writing a Visual Studio extension now :) – Earlz Jan 09 '13 at 17:13
  • I marked you up. This is exactly what i was looking for. – Justin Oct 22 '14 at 21:35
  • @Justin tnx, we're releasing a new version in december. If you have any questions or suggestion, please write to me. – Mikhail Shcherbakov Oct 23 '14 at 20:00