Causing VS2010 debugger to break when Debug.Assert fails

Is there any way to cause Visual Studio 2010 to break while debugging when the argument of Debug.Assert evaluates to false ?

Example: in my code I have lines like this:

Debug.Assert(!double.IsInfinity(x));

If I am not debugging, a window pops up when the assertion fails.

But when I am debugging, the assertion is logged to the "Output" pane, which is easy to miss; there is not popup window and the debugger does not stop. Therefore: is there any way to force the Visual Studio debugger to break when Debug.Assert fails?

(BTW: I am developing a WPF based desktop application. In a Windows Forms application, the behavior seems to be differnt: here, the debugger stops on Debug.Assert .)

EDIT : Please let me clarify: I am not looking for an alternative to Debug.Assert(), because my code and external code I use is full of Debug.Assert() statements. I am looking for a way to cause the Visual Studio debugger to break when Debugg.Assert fails. (I think earlier VS versions did that, and that the behavior changed in VS2010).


If by debugging you mean using "Step Into" feature, see this MS's answer to the problem.

http://connect.microsoft.com/VisualStudio/feedback/details/522995/system-diagnostics-debug-assert-doesnt-pop-up-messagebox-correctly-when-step-into-it-in-vs-2010-and-2008

Using "Step Over" at the Assert statement does solve the issue too (for me).


您可以使用条件断点来处理这种行为,只需在要断开的行上设置断点,然后右键单击左侧的点并选择条件,在弹出式菜单中输入所需的条件(在您的情况下为双倍)。 IsInfinity(X))


Use the Debugger.Break method (in the System.Diagnostics namespace). This will break execution if you are running in a debugger.

链接地址: http://www.djcxy.com/p/27976.html

上一篇: VS2010没有打破静态构造函数异常的调试

下一篇: 当Debug.Assert失败时导致VS2010调试器中断