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

元气小坏坏 提交于 2019-12-22 11:25:37

问题


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].


回答1:


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.

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




回答2:


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



来源:https://stackoverflow.com/questions/40151182/can-i-get-visual-studio-to-ignore-a-specific-instance-of-an-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!