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