damian
damian

Reputation: 3674

Can I get Visual Studio to ignore a specific *instance* of an exception?

I am working with 3rd-party code that throws and catches a NullReferenceException as part of its ordinary, correct operation. I would like to be able to tell Visual Studio to ignore this instance (ie, ignore NullReferenceExceptions thrown from this .cs file + line number) but continue to break on other thrown NullReferenceExceptions.

Is this possible?

edit: By 3rd party code I mean source code that is part of the project, but that I don't own and won't be modifying. I can't use anything that relies on VS's definition of user code, for example, because this also counts as user code. The size of the project means that this detail is out of my control. For similar reasons I don't want to add [DebuggerHidden].

Upvotes: 2

Views: 1478

Answers (2)

Omer Raviv
Omer Raviv

Reputation: 11827

As others have mentioned, if an exception is truly caught and handled inside 3rd party code (where the debugger's definition of "3rd party code" is code that is in an assembly which is non-optimized AND we don't have .pdbs for), then to avoid the debugger from stopping on it, you simply need to go to Tools->Options->Debugging->General and enable "Just My Code".

If the code in question is not 3rd party code, you can add a DebuggerNonUserCode attribute to it, to control whether the debugger will break on an exception in the decorated method (again, assuming "Just My Code" is enabled).

In VS "15" Preview 5, you can actually disable breaking on an exception when it's thrown inside a specific module, but that's not available in VS2015.

Another option to workaround this quickly, is to use OzCode which has a ToolBar button for toggling "Break on All CLR Exceptions". This is a really quick way of enabling/disabling all exceptions, which you can toggle before you know the annoying NullReferenceException is about to occur. Break on all CLR exceptions

Disclaimer: I'm co-creator of the tool I just mentioned.

Upvotes: 5

JohnChris
JohnChris

Reputation: 1360

If I understand you correctly, you don't want your code to stop being executed after a certain exception which occurs when you throw the exception. If you want to do this just put your try and catch block over that code and don't use the throw statement at all

         try
     { string strX  = textbox.Text
       int x = Convert.ToInt32(strX);
     }
    catch (FormatException){}
    ...rest of code

If you put a throw statement it would exit the block of code it is in

Upvotes: 1

Related Questions