Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ignore certain exceptions on certain lines in Visual Studio?

Tags:

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

like image 974
Earlz Avatar asked Jan 08 '13 16:01

Earlz


People also ask

How do I ignore exceptions in Visual Studio?

When you run Visual Studio in debug mode, there are Exception Setting on the bottom tool bar. Once you click it, there are all type exceptions. Uncheck exceptions that you want.

What is exception settings Visual Studio?

In Visual Studio, you can use Exception Settings Window to manage the exceptions – for which exception to break, at which point to break, to add or deleting exceptions. The Exception Settings windows for Visual Studio is there for quite some time.


2 Answers

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

like image 91
Elastep Avatar answered Oct 19 '22 22:10

Elastep


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.

like image 41
Jaster Avatar answered Oct 19 '22 21:10

Jaster