Can Debug.Assert be set to just throw an exception instead of popping up a window in C #?

I would like an exception to be thrown and the editor jump to the place whenever the statement did not work in my C # application. But instead, the abort / retry / ignore message box appears. Is there a way to control this behavior?

+4
source share
2 answers

Not directly: this is what Debug.Assert does. See Documentation

http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.aspx

Perhaps you want to use something other than Debug.Assert? For example, just throw an exception if it cannot be handled, or by calling a specific onError handler defined by the application?

In addition, you can get what you want by adding a listener and connecting to this listener. See the documentation.

+3
source

You can add a custom TraceListener that throws an exception in any of the .Fail() methods:

 public class ThrowListener : TextWriterTraceListener { public override void Fail(string message) { throw new Exception(message); } public override void Fail(string message, string detailMessage) { throw new Exception(message); } } 

I would probably get my own Exception type, which accepts both the message and detailMessage, and so it becomes more obvious where the exception comes from.

You will need to expand the call stack before calling .Assert (), since the debugger will most likely lead you to throw .

You can add it like this:

 Debug.Listeners.Insert(0, new ThrowListener()); 
+3
source

All Articles