Debug.Assert is causing my application to exit
I created a library with many utility functions to perform some tasks and placed it in it's own DLL.
Throughout the methods in this library, i've placed Debug.Assert statements to verify the state of the application.
开发者_StackOverflow社区The problem is, when the Assert is firing (the condition is false), it presents the dialog, clicking "Abort" on it causes the entire application that uses this DLL to crash.
This poses 2 questions:
Can this be avoided? i'd expect this to throw an exception without exiting the application.
How come a DLL can cause the application that loads it to exit? isn't this a security breach?
Thanks
The Debug.Assert is not intended to end up in release code (hence 'Debug' :) ). Choosing Abort means kill the process that raised the assert. If you choose Ignore, it should carry on, and either way this should not happen in the Release build as the Debug.Assert will be stripped out.
Edit: Here's a link to the MSDN explanation: http://msdn.microsoft.com/en-us/library/e63efys0.aspx
If you want to have an exception thrown, why dont you just throw an exception?
Change
Assert(var);
to
if(!var)
throw new Exception("var was false");
Assert will leave the application, assertion are here to protect you from coder's mistake. If a function should never receive a null pointer, and if it does then it would be a mistake, then an assert would be the right tool.
If you want to express an exceptionnal situation, than the exception is the tool you are looking for
精彩评论